Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active November 6, 2015 03:30
Show Gist options
  • Save relyky/7a6af710a4be0e1b5520 to your computer and use it in GitHub Desktop.
Save relyky/7a6af710a4be0e1b5520 to your computer and use it in GitHub Desktop.
VB6 計算中文字串長度
'參考文章:
'http://blog.xuite.net/moonvilla/mypcnote/43694699-%E8%A8%88%E7%AE%97%E4%B8%AD%E6%96%87%E5%AD%97%E4%B8%B2%E9%95%B7%E5%BA%A6
'http://ctnet.ctsh.mlc.edu.tw/ctpcm/Dhtml/VB_EX/vbext005.htm
' 欲計算字串 S 的長度
N = 0
For I = 1 To Len(S)
C = Asc(Mid(S, I, 1)) ' 取得第 I 個字元組的字元碼
If C >= 0 And C < 128 Then ' 英文
N = N + 1
Else ' 中文
N = N + 2
End If
Next
'補充說明:除了以上方法之外,以下是更快速的計算長度方法:
LenB(StrConv("中英Mixed", vbFromUnicode)) ' => 9, 符合此需求
'長度計算比對指令
Len("中英Mixed") ' => 7
LenB("中英Mixed") ' => 14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment