※ 引述《Cypresslin (啊哈哈~~)》之銘言:
: 請問一下
: 因為在電腦裡面中文字是佔兩個長度
: 那這樣的話如果我跑
: data="科科test"
: msgbox(Len(data))
: 的結果,它會因為文字為Unicode的關係而告訴我答案是6
: (我想要的答案是8)
: 那如果我要的值是他的位元組數有沒有其他的辦法呢
: LenB這個好像方法被取消了...
這是我寫的
''' <summary>
''' 計算字串的所佔 bit 長度
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function StringLength(ByVal str As String) As Integer
Dim strbytes() As Byte
Dim tmpcnt As Integer = 0
For ii As Integer = 0 To str.Length - 1
strbytes = Encoding.UTF8.GetBytes(str.Substring(ii, 1))
If strbytes.Length = 3 Then
tmpcnt = tmpcnt + 2
Else
tmpcnt = tmpcnt + 1
End If
Next
Return tmpcnt
End Function
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.167.35.207