for i=1 to len(sAddress) if asc(mid(saddress,i,1))<0 then slen=slen+2 else slen=slen+1 end if next i
Function GetCharCount(sIn As String, lWideByteCnt As Long, lNarrowByteCnt As Long) As Long On Error GoTo ErrorHandler Dim lTotalByteCnt As Long GetCharCount = False
字符串的ASCII码实际上只是第一个字符的ASCII。倘若想区分字符串中某字符是中文还是英文,方法有很多。 如 ch = Mid(strString, I, 1) 可以判断其长度: If LenB(StrConv(ch, vbFromUnicode)) = 2 Then 中文字符 Else 英文字符 End If 还可以使用Asscii码来判断 If Abs(Asc(ch)) > 256 Then 中文字符 Else 英文字符 End If
LenB(StrConv("广东珠海建业5路108号", vbFromUnicode))
Dim B() As Byte Dim I As Integer Dim HexCode As String B = StrConv("广东珠海建业5路108号", vbFromUnicode) HexCode = Empty For I = 0 To Ubound(B) HexCode = HexCode & Right("0" & Hex(B(I)), 2) Next
if asc(mid(saddress,i,1))<0 then
slen=slen+2
else
slen=slen+1
end if
next i
On Error GoTo ErrorHandler
Dim lTotalByteCnt As Long
GetCharCount = False
lTotalByteCnt = LenB(StrConv(sIn, vbFromUnicode))
lNarrowByteCnt = LenB(sIn) - lTotalByteCnt
lWideByteCnt = (lTotalByteCnt - lNarrowByteCnt) / 2
GetCharCount = lTotalByteCnt
Exit Function
ErrorHandler:
End Function
lWideByteCnt返回的是宽字符的个数,lNarrowByteCnt返回的是窄字符的个数sAddress="广东珠海建业5路108号"
i=GetCharCount(sAddress,i1,i2)得到:i=20,i1=8,i2=4
如
ch = Mid(strString, I, 1) 可以判断其长度:
If LenB(StrConv(ch, vbFromUnicode)) = 2 Then
中文字符
Else
英文字符
End If 还可以使用Asscii码来判断
If Abs(Asc(ch)) > 256 Then
中文字符
Else
英文字符
End If
Dim I As Integer
Dim HexCode As String
B = StrConv("广东珠海建业5路108号", vbFromUnicode)
HexCode = Empty
For I = 0 To Ubound(B)
HexCode = HexCode & Right("0" & Hex(B(I)), 2)
Next