回 帖 发 新 帖 刷新版面

主题:谁帮我解释下网上常见函数strLength,该函数的作用是:求字符串长度,汉字算两个字符,英文算一个

函数内容:
function strLength(str)
    ON ERROR RESUME NEXT
    dim WINNT_CHINESE
    WINNT_CHINESE    = (len("中国")=2)
    if WINNT_CHINESE then
        dim l,t,c
        dim i
        l=len(str)
        t=l
        for i=1 to l
            c=asc(mid(str,i,1))
            if c<0 then c=c+65536
            if c>255 then
                t=t+1
            end if
        next
        strLength=t
    else 
        strLength=len(str)
    end if
    if err.number<>0 then err.clear
end function
主要是这部分:for i=1 to l
            c=asc(mid(str,i,1))
            if c<0 then c=c+65536
            if c>255 then
                t=t+1
            end if

回复列表 (共2个回复)

沙发

因为如果是中文的话,asc(汉字)出来的结果是负数。其实这段主要是为了判断是否是中文字符,当然你也可以不必这么写
if c<0 or c>255 then 是一样的。

板凳

谢楼上的!

我来回复

您尚未登录,请登录后再回复。点此登录或注册