|
问题,希腊字符,应该是双字节,还是单字节呢,为什么?
不懂看下面测试
Sub TestLenDB()
Str = "ΑΒΓαβγ" '希腊字符,应该是双字节,还是单字节呢
Debug.Print LenDB(Str), LenEx(Str), LenEx1(Str)
End Sub
返回单双字节的三个算法
Public Function LenEx(VarString As Variant) As Variant
'算法一
LenEx = LenB(StrConv(VarString, vbFromUnicode)) '将Unicode转换为ANSI,然后再计算字节长度
End Function
'算法二
Public Function LenEx1(VarString As Variant) As Variant
Dim lLen As Integer, i As Integer
LenEx1 = 0
lLen = Len(VarString)
If lLen Then '如长度不为零
For i = 1 To lLen
If Asc(Mid(VarString, i, 1)) < 0 Then
LenEx1 = LenEx1 + 2
Else
LenEx1 = LenEx1 + 1
End If
Next
End If
End Function
'算法三 直接操作对比Unicode码
Function LenDB(Str As String) As Long
Dim i As Long, lngLenB As Long
Dim abytStr() As Byte
Const DoubleByte As Byte = &H30
abytStr = Str
lngLenB = LenB(Str)
For i = 1& To lngLenB Step 2&
If abytStr(i) < DoubleByte Then
lngLenB = lngLenB - 1&
End If
Next
LenDB = lngLenB
End Function
|
|