Option ExplicitPrivate Sub Command1_Click() Dim strP As String strP = "abcdÖйú" Debug.Print Len(strP) Debug.Print LenB(strP) End Sub
'用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
==================== 这个函数统计不知道是否准确? 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
如果你需要计算字符串的字节长度,也就是占用多少个字节,那么用LenB
Option ExplicitPrivate Sub Command1_Click()
Dim strP As String
strP = "abcdÖйú"
Debug.Print Len(strP)
Debug.Print LenB(strP)
End Sub
'用下面一个函数好象是可以的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
这个函数统计不知道是否准确?
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
1、如果只是单纯地统计字数,用Len就可以了,1个汉字算1个字符。这1点跟Office Word统计字数的方法是一样的。字符串“Xcf中文gh中dfghf中sd国gh”算19个字符。
2、1个中文汉字和1个英文字母都是2个字节。用Lenb("Xcf中文gh中dfghf中sd国gh")=38字节。