private function myleft(byval str as string,byval n as integer) as string while len(str)>0 and n>0 if lenb(left(str,1))=1 then myleft=myleft & left(str,1) n=n-1 str=right(str,len(str)-1) else if n>1 then myleft=myleft & left(str,1) n=n-2 str=right(str,len(str)-1) else n=0 end if end if end while end function
Dim b As String Dim c As String b = StrConv("中国and美国", vbFromUnicode) c = StrConv(LeftB(b, 8), vbUnicode) If Len(c) > 0 Then If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1) Debug.Print c End Ifb = StrConv("中国和美国", vbFromUnicode) c = StrConv(LeftB(b, 8), vbUnicode) If Len(c) > 0 Then If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1) Debug.Print c End If
换句话说如果第8个字节是汉字就不截取或者加一个空格,但是不能发生截取半个汉字的情况
while len(str)>0 and n>0
if lenb(left(str,1))=1 then
myleft=myleft & left(str,1)
n=n-1
str=right(str,len(str)-1)
else
if n>1 then
myleft=myleft & left(str,1)
n=n-2
str=right(str,len(str)-1)
else
n=0
end if
end if
end while
end function
Dim c As String
b = StrConv("中国and美国", vbFromUnicode)
c = StrConv(LeftB(b, 8), vbUnicode)
If Len(c) > 0 Then
If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1)
Debug.Print c
End Ifb = StrConv("中国和美国", vbFromUnicode)
c = StrConv(LeftB(b, 8), vbUnicode)
If Len(c) > 0 Then
If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1)
Debug.Print c
End If