通常在顯示新聞標題的時候,有時候標題中會出現漢字和英文共存,直接用left截字符串會出現兩條新聞標題長度不一,使用這個函數可以解決這個問題。
<%
'**************************************************
'函數名:gotTopic
'作 用:截字符串,漢字一個算兩個字符,英文算一個字符
'參 數:str ----原字符串
' strlen ----截取長度
'返回值:截取後的字符串
'**************************************************
Function gotTopic(ByVal str, ByVal strlen)
If str = "" Then
gotTopic = ""
Exit Function
End If
Dim l, t, c, i, strTemp
str = Replace(Replace(Replace(Replace(str, " ", " "), """, Chr(34)), ">", ">"), "&lt;", "<")
l = Len(str)
t = 0
strTemp = str
strlen = CLng(strlen)
For i = 1 To l
c = Abs(Asc(Mid(str, i, 1)))
If c > 255 Then
t = t + 2
Else
t = t + 1
End If
If t >= strlen Then
strTemp = Left(str, i)
Exit For
End If
Next
If strTemp <> str Then
strTemp = strTemp & "…"
End If
gotTopic = Replace(Replace(Replace(Replace(strTemp, " ", " "), Chr(34), """), ">", ">"), "<", "&lt;")
End Function
%>
<%
str="一共11111w有漢字"
str1="一共有五漢字"
response.write "gotTopic
"
response.write gotTopic(str,10)&"
"&gotTopic(str1,10)&"
"
response.write "left
"
response.write Left(str,5)&"
"&Left(str1,5)
response.end
%>