如果一个字符串中含有汉字,怎么判断汉字个数?

解决方案 »

  1.   

    Function LenTrue(str)
    Dim StrLen
    Dim i,l
    StrLen=0
    l=Len(str)
    For i=1 To l
    c=asc(mid(str,i,1))
    IF c<0 Then c=c+65536 End IF
    IF c>255 then
    StrLen=StrLen+2
    Else
    StrLen=StrLen+1
    End IF
    Next
    LenTrue=StrLen
    End Function知道怎麼求字符真正長度後求漢字個數就不用我說了吧
      

  2.   

    dim strTemp as string 
    strTemp="adg我们dafsd的中sdf"
    debug.print lenb(strconv(strTemp,vbFromUnicode))-len(strTemp)
      

  3.   

    sorry:dim strTemp as string 
    strTemp="adg我们dafsd的中sdf"
    debug.print len(strconv(strTemp,vbFromUnicode))-len(strTemp)
      

  4.   

    ft~~~~~~~应该还是第一个,呵呵
    lenb(strconv(strTemp,vbFromUnicode))-len(strTemp)
      

  5.   

    Dim total As Long
            Dim count As Long
            Dim i As Long
            count = 0
            total = Len(str)
            For i = 1 To total
                    If Asc(Mid(str, i, 1)) < 0 Then
                          count = count + 1
                    End If
            Next
            LenTrue = count
      

  6.   

    补充一点,如果你的程序中只有中文和英文字符的话,上面二位的程序都可行,如果还包含其它的unicode编码的字串,上面的程序会把其它的unicode编码的字串(如日文,韩文等)也当成中文这时候,就没有好的办法,只能逐一取出字符根据它的unicode编码,判断它是否为汉字。
      

  7.   

    关于unicode编码,可以参考:
    http://www.xlmz.net/forum/viewthread.php?tid=2036&sid=Y5k07Bsk