问题提得不严谨,应该是如何判断字串中是否有中英文字符吧?先用mid函数逐一取出各个字符,然后用asc函数取得字符的ascii值,一般简单的认为字符的ascii值小于0,就为中文。简单的示例: dim s as string s="abc中国vass" dim i as long dim temps as string for i=1 to len(s) temps=mid(s,i,1) if asc(temps)<0 then debug.print "第"+cstr(i)+"个字符为中文" else debug.print "第"+cstr(i)+"个字符为英文" end if next
很难,vb使用的是uncode,我想只好用循环 每一位判断了
分割中英文的原理和上面相似,简单的示例: dim s as string s="abc中国vass" dim i as long dim chs as string chs="" dim ens as string ens="" dim temps as string for i=1 to len(s) temps=mid(s,i,1) if asc(temps)<0 then chs=chs+temps else ens=ens+temps end if next debug.print "中文:"+chs debug.print "英文:"+ens
否有中英文字符的判断function fncChineseChk(strIn as sting) as boolean if LenB(strIn) > Len(strIn) then fncChineseChk = True else fncChineseChk = False endif end function
ascii小于0就是汉字,是规则吗?
给之前古林的 以下是我在debug窗口里面的结果 b = "a" ?lenb(b) 2 ?len(b) 1 ?fncChineseChk("a") Truelenb跟len到底是什么区别?同样是半角字符,为何会是两倍关系?
可以自己定义一个函数来取字符串长度Public Function Lench(ByVal TmpStr As String) As String Lench = LenB(StrConv(TmpStr, vbFromUnicode)) End Functionif Lench(字符串)=len(字符串) then msgbox "英文字符串" else msgbox "含有中文的字符串" end if
dim s as string
s="abc中国vass"
dim i as long
dim temps as string
for i=1 to len(s)
temps=mid(s,i,1)
if asc(temps)<0 then
debug.print "第"+cstr(i)+"个字符为中文"
else
debug.print "第"+cstr(i)+"个字符为英文"
end if
next
dim s as string
s="abc中国vass"
dim i as long
dim chs as string
chs=""
dim ens as string
ens=""
dim temps as string
for i=1 to len(s)
temps=mid(s,i,1)
if asc(temps)<0 then
chs=chs+temps
else
ens=ens+temps
end if
next
debug.print "中文:"+chs
debug.print "英文:"+ens
if LenB(strIn) > Len(strIn) then
fncChineseChk = True
else
fncChineseChk = False
endif
end function
以下是我在debug窗口里面的结果
b = "a"
?lenb(b)
2
?len(b)
1
?fncChineseChk("a")
Truelenb跟len到底是什么区别?同样是半角字符,为何会是两倍关系?
大于零 能显示的是英文字符 数字等
Lench = LenB(StrConv(TmpStr, vbFromUnicode))
End Functionif Lench(字符串)=len(字符串) then
msgbox "英文字符串"
else
msgbox "含有中文的字符串"
end if