VB 汉字字符串的截取LEN、LEFT、RIGHT

前端之家收集整理的这篇文章主要介绍了VB 汉字字符串的截取LEN、LEFT、RIGHT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

自从VB4.0后,就把汉字看做是1个字符了。这给我们的字符串的截取带来了严重的麻烦!

如果是汉字+数字或字母的字符串,往往截取到的字符串长度不是理想中的,其中涉及到的函数有LEN、LEFT、RIGHT我自己编写了如下3个函数,用来取代 len()、left()、right(),希望能解决您的问题。'--------------------------------------------------------'Name:lenX'Argument:uStr'Return:'Description:返回字符串的长度,1个中文字符长度为2'--------------------------------------------------------function lenX(byval uStr)dim theLen,x,testuStrtheLen = 0for x = 1 to len(uStr)testuStr = mid(uStr,1)if asc(testuStr) < 0 thentheLen = theLen + 2elsetheLen = theLen + 1end ifnextlenX = theLenend function'--------------------------------------------------------'Name:leftX'Argument:uStr待处理的字符串'uLen要截取的长度'Return:'Description:返回指定长度的字符串,1个中文字符长度为2'--------------------------------------------------------function leftX(byval uStr,byval uLen)dim i,j,uTestStr,theStrleftX = ""j = 0for i = 1 to len(uStr)uTestStr= mid(uStr,i,1)theStr= theStr & uTestStrif asc(uTestStr) < 0 thenj = j + 2elsej = j + 1end ifif j >= uLen then exit fornextleftX = theStrend function'--------------------------------------------------------'Name:rightX'Argument:uStr待处理的字符串'uLen要截取的长度'Return:'Description:返回指定长度的字符串,1个中文字符长度为2'--------------------------------------------------------function rightX(byval uStr,uTestStrrightX = ""j = 0for i = len(uStr) to 1 step -1uTestStr = mid(uStr,1)rightX = rightX & uTestStrif asc(uTestStr) < 0 thenj = j + 2elsej = j + 1end ifif j >= uLen then exit fornextend function

原文链接:https://www.f2er.com/vb/262842.html

猜你在找的VB相关文章