分类分类
关注+2010-02-05作者:
解决left截取字符标题长度不一的办法:
我们经常看到直接用left截字符串时,因为标题中会出现汉字和英文共存,所以造成两条新闻标题长度不一,为了解决这个问题我们可以使用下面这个函数。
| <% ’************************************************** ’函数名:gotTopic ’作 用:截字符串,汉字一个算两个字符,英文算一个字符 ’参 数:str ----原字符串 ’ strlen ----截取长度 ’返回值:截取后的字符串 ’Copyright:asp教程 ’************************************************** 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)), ">", ">"), "<", "<") 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), """), ">", ">"), "<", "<") 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 %> |
相关文章
更多+相同厂商
热门推荐
点击查看更多
点击查看更多
点击查看更多
说两句网友评论