当前位置:asp编程网>技术教程>Asp教程>  正文

截字符串,汉字一个算两个字符,英文算一个字符

2007-11-14 18:02:15   来源:网络    作者:佚名   浏览量:2526   收藏
通常在显示新闻标题的时候,有时候标题中会出现汉字和英文共存,直接用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)), ">", ">"), "&#38;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 &#38; "…"
    End If
    gotTopic = Replace(Replace(Replace(Replace(strTemp, " ", " "), Chr(34), """), ">", ">"), "<", "&#38;lt;")
End Function
%>
 <%
 str="一共11111w有汉字"
 str1="一共有五汉字"
 response.write "gotTopic
"
 response.write gotTopic(str,10)&#38;"
"&#38;gotTopic(str1,10)&#38;"
"
 response.write "left
"
 response.write Left(str,5)&#38;"
"&#38;Left(str1,5)
 response.end
%>


关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网