中文字符串长度:字符串中文的问题来源: 发布时间:星期四, 2009年2月12日 浏览:85次 评论:0
字串中文问题起於vb字串是使用unicode而我们般是使用ascii code 这差别在何处呢?unicode每个字元长度是2个而ascii是个如果说我将们将vb字串写入档案有时会有意想不到结果例如: text1.text = \"这是个abc\" len5 = len(str5) 如果我们access资料库有栏位长度是10个所以我们在textbox中设定maxlength = 10但是上面例子得到len5是7而不是我们认为11不管是中文或英文vb律以unicode来存所以str5长度是7个\"字元\"而text1最大长度限制是107没有超过10故使用者仍可输入但存档时11个超过10个所以会有错 可是或许有人发现使用rs232来传资料时另端主机是ascii编码机器在vb中我们若使用来传样可以通啊其实那是vb在传送和接收data时会做转换使我们程式设计较方便但如果传资料是binary时就头大啦例如说以字串方式来传送资料当想传ascii 大於128时常有些问题asc(chr(129))=0使我们不能用chr指令来放资料(事实上您可以使用chrw(129)来存资料和使用ascw来取得值加个w代表是word运算)这时候就只有使用 .gif' />来做了1.unicode转成arydim ary as dim str5 as dim i as long str5 = \"这abc\" ary = str5 for i = lbound(ary) to ubound(ary) debug.pr ary(i) \'得 25 144 97 0 98 0 99 0 next i debug.pr len(str5), lenb(str5) \'得4 8 所以了可看出unicode 特性程式应改下使用strconv来转换 dim ary as dim str5 as dim i as long str5 = \"这abc\" ary = strconv(str5, vbfromunicode) for i = lbound(ary) to ubound(ary) debug.pr ary(i) \'得 25 144 97 98 99 next i debug.pr lenb(strconv(str5, vbfromunicode)) \'得5 2.ary转回unicode 使用strconv转换 dim ary(10) as dim str5 as ary(0) = 25 ary(1) = 144 ary(2) = 97 ary(3) = 98 ary(4) = 99 str5 = strconv(ary, vbunicode)3.些有用函式substr 中文化取子字串相对mid strlen 中文化字串长度相对len strleft 中文化取左字串相对left strright 中文化取右字串相对right ischinese check某个字是否中文字 public function substr(byval tstr as , start as eger, optional leng as variant) as dim tmpstr as ismissing(leng) then tmpstr = strconv(midb(strconv(tstr, vbfromunicode), start), vbunicode) tmpstr = strconv(midb(strconv(tstr, vbfromunicode), start, leng), vbunicode) end substr = tmpstr end function public function strlen(byval tstr as ) as eger strlen = lenb(strconv(tstr, vbfromunicode)) end function public function strleft(byval str5 as , byval len5 as long) as dim tmpstr as tmpstr = strconv(str5, vbfromunicode) tmpstr = leftb(tmpstr, len5) strleft = strconv(tmpstr, vbunicode) end function public function strright(byval str5 as , byval len5 as long) as dim tmpstr as tmpstr = strconv(str5, vbfromunicode) tmpstr = rightb(tmpstr, len5) strleft = strconv(tmpstr, vbunicode) end function public function ischinese(byval asciiv as eger) as boolean len(hex$(asciiv)) > 2 then ischinese = true ischinese = false end end function 0
相关文章读者评论发表评论 |
|