方法一:
在文章列表的時候,如果標題過長,往往會撐破表格破壞頁面形象。一般做法是略去超長部分而以省略符號代替。比如要取前10個字元,則可寫出以下語句:
if Len(title)>10 then title=Left(title,9)+"…"
'湊起來剛好10個字元
而我們中國人要面對現實——漢字寬度是字母的兩倍。所以得設計一個函數,用一個變數計算字串長度,如果遇到的是字母,這個長度就加1,如果遇到的是漢字,長度加2:
FUNCTION cuttitle(str,strlen)
'str為待切標題,strlen為截取長度(按字母計算)
dim tmplen,tmpstr,i,s
tmpstr=""
tmplen=0
str=trim(str)
if str="" then exit function
for i=1 to Len(str)
s=mid(str,i,1)
tmpstr=tmpstr&s
tmplen=tmplen+1
if Asc(s)<0 then tmplen=tmplen+1
'如果是漢字,長度再加1
if tmplen>strlen then
tmpstr=Left(tmpstr,Len(tmpstr)-2)&"…"
exit for
end if
next
cuttitle=tmpstr
END FUNCTION
----------------------------------------------------------------------------
方法二:
<%
Function GetLen(Str) '檢測包含漢字字串的長度,一個漢字的長度為2,英文字母的長度為1
Dim Strlen,i
GetLen = 0
Strlen=Len(Str)
IF IsNull(Str) Then Exit Function End If
For i = 1 To Strlen
If Asc(Mid (Str,i,1)) < 0 Then
GetLen = GetLen + 2
Else
GetLen = GetLen + 1
End If
Next
End Function
Function GetLeft(Str,L,Alter) '擷取字串的長度並根據要求裁切,按漢字的長度計算,1個漢字的長度=2個英文字母的長度,其中str表示呆截取的字串變數或常量,L表示截取後保留的長度,Alter表示截取後補的字元,一般為"....".
Dim i,j
j=1
GetLeft = ""
If GetLen(Str)>2*L Then
For i=1 To L
If Asc(Mid(Str,j,1)) < 0 or Asc(Mid(Str,j+1,2)) < 0 Then
GetLeft=GetLeft & Mid(Str,j,1)
j=j+1
Else
GetLeft=GetLeft & Mid(Str,j,2)
j=j+2
End If
Next
If Alter<>"" Then
GetLeft = GetLeft & Alter
End If
Else
GetLeft = Str
End If
End Function
%>
其中Function GetLen(Str)函數是計算字串長度的,我的計算規則是一個漢字的長度為2,英文字母的長度為1.
Function GetLeft(Str,L,Alter) 函數是截斷的函數.
譬如要截斷字串"代碼測試",使之只保留"代碼",然後後補以"....",那麼只需要
<%
GetLeft("代碼測試",2,"....")
%>
---------------------------------------------------------------------------------
方法三:(截取中英文字串)
<%
function strLeft(str,num)
dim p_str,p_num
p_str = ""
p_num = 0
if trim(str) <> "" then
p_len = len(str)
for i = 1 to p_len
if asc(mid(str,i,1)) > 255 or asc(mid(str,i,1)) < 0 then
p_num = p_num + 2
else
p_num = p_num + 1
end if
if p_num > num then
p_str = Left(str,i-1)
exit for
else
p_str = str
end if
next
end if
strLeft=p_str
end function
%>
______________________________________________
截取月日:
<%= Month(now()) %>-<%= day(now()) %>
function date_md(tim)
dim m,d
if Month(tim)>9 then
m=Month(tim)
else
m="0"&Month(tim)
end if
if day(tim) >9 then
d=day(tim)
else
d="0"&day(tim)
end if
date_md=m&"-"&d
end function