用Len计算字符串长度时,如果里面有中文,计算不准确。

解决方案 »

  1.   

    如果你需要计算字符串的长度,也就是占用多少个字,那用Len
    如果你需要计算字符串的字节长度,也就是占用多少个字节,那么用LenB
      

  2.   


    Option ExplicitPrivate Sub Command1_Click()
        Dim strP As String
        strP = "abcdÖйú"
        Debug.Print Len(strP)
        Debug.Print LenB(strP)
    End Sub
      

  3.   

    '用LENB(strconv(youstring,vbfromunicode)) 或者
    '用下面一个函数好象是可以的Private Function prvStringLenth(strString As String) As LONG
    Dim lngI As LONG
    Dim lngLenth As LONGprvStringLenth = 0
    lngLenth = 0If Len(Trim(strString)) > 0 Then
    For lngI = 1 To Len(Trim(strString))
    If Asc(Mid(Trim(strString), lngI, 1)) < 0 Then
    lngLenth = lngLenth + 2 '如果asc码小于0则为汉字
    Else
    lngLenth = lngLenth + 1 '大于0则为英文
    End If
    Next
    prvStringLenth = lngLenth
    End IfEnd Function
      

  4.   

    ====================
    这个函数统计不知道是否准确?
    Private Function prvStringLenth(strString As String) As LONG
    Dim lngI As LONG
    Dim lngLenth As LONGprvStringLenth = 0
    lngLenth = 0If Len(Trim(strString)) > 0 Then
    For lngI = 1 To Len(Trim(strString))
    If Asc(Mid(Trim(strString), lngI, 1)) < 0 Then
    lngLenth = lngLenth + 2 '如果asc码小于0则为汉字
    Else
    lngLenth = lngLenth + 1 '大于0则为英文
    End If
    Next
    prvStringLenth = lngLenth
    End IfEnd Function
      

  5.   

    看你的函数,像是统计字数。
    1、如果只是单纯地统计字数,用Len就可以了,1个汉字算1个字符。这1点跟Office Word统计字数的方法是一样的。字符串“Xcf中文gh中dfghf中sd国gh”算19个字符。
    2、1个中文汉字和1个英文字母都是2个字节。用Lenb("Xcf中文gh中dfghf中sd国gh")=38字节。
      

  6.   

    有个API不是可以显示中英混合字符串长度的。记不到了。你搜索一下。