作者mywheat (麦田)
看板Visual_Basic
标题Re: [.NET] 字串长度的判断问题
时间Sun Oct 19 01:51:18 2008
※ 引述《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