这里有一个简单的方法:
Function Len_Str(Strs As String) As Long
Dim ReLen As Long
Dim I As Long
ReLen=0
For I=1 To Len(Strs)
'属于ASCII码
If (Asc(Mid$(Strs,I,1))>=0) And (Asc(Mid$(Strs,I,1))<=255) Then
ReLen=ReLen+1
'不'属于ASCII码
Else
ReLen=ReLen+2
End If
Next
Len_Str=ReLen
End Function
Public Function GetByteLen(ByVal str As String) As Long
Dim I As Long
Dim a() As Byte
GetByteLen = 0
str = Trim(str)
a = str
For I = UBound(a) To 0 Step -1
If I Mod 2 Then
If a(I) > 0 Then
GetByteLen = GetByteLen + 1
End If
Else
GetByteLen = GetByteLen + 1
End If
Next
End Function
Function LeftMbcs(ByVal StrArg As String, ByVal arg1 As Long) As String
AnsiLeftB = StrConv(LeftB(StrConv(StrArg, vbFromUnicode), arg1), vbUnicode)
End Function
应该如此:
Function AnsiStrConv(StrArg, flag)
nsiStrConv = StrConv(StrArg, flag)
End Function
' LeftB ANSI Unicode
Function AnsiLeftB(ByVal StrArg As String, ByVal arg1 As Long) As String
AnsiLeftB = AnsiStrConv(LeftB(AnsiStrConv(StrArg, _
vbFromUnicode), arg1), vbUnicode)
End Function
详细请见MSDN:“Differences in String Function Operations”
The example uses LenB and a user-defined function (LenMbcs) to return the number of byte characters in a string if ANSI is used to represent the string.
Function LenMbcs (ByVal str as String)
LenMbcs = LenB(StrConv(str, vbFromUnicode))
End Function
Dim MyString, MyLen
MyString = "ABc" '//将 MyString = "中国人Good" 更清楚一些
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString) '// 7
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString) '// 14
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString) '// 10
' Returns 5 - 5 bytes used for ANSI.