Skip to content

2006 年 10 月 13 日

正确截取中文字符长度的len及left函数

vbs中自带的len及left函数无法对中文字符长度进行正确的截取,可使用下2函数替代。

  1. <%
  2. Function Strleft(Str,L)
  3. Temp_Str=Len(Str)
  4. For I=1 To Temp_Str
  5. Test_Str=(Mid(Str,I,1))
  6. Strleft=Strleft&Test_Str
  7. If Asc(Test_Str)>0 Then
  8. lens=lens+1
  9. Else
  10. lens=lens+2
  11. End If
  12. If lens>=L Then Exit For
  13. Next
  14. End Function
  15.  
  16. function strLength(str)
  17. ON ERROR RESUME NEXT
  18. dim WINNT_CHINESE
  19. WINNT_CHINESE = (len("中国")=2)
  20. if WINNT_CHINESE then
  21. dim l,t,c
  22. dim i
  23. l=len(str)
  24. t=l
  25. for i=1 to l
  26. c=asc(mid(str,i,1))
  27. if c<0 then c=c+65536
  28. if c>255 then
  29. t=t+1
  30. end if
  31. next
  32. strLength=t
  33. else
  34. strLength=len(str)
  35. end if
  36. if err.number<>0 then err.clear
  37. end function
  38. %>
Read more from Website

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

mugen 2d fighting games
Weboy
WordPress Themes