
正文
vb.net取字符串长度 vb字符长度函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
VB.NET如何截取字符串最后几位
'这个当然可以,用mid 取字符串的位置长度
Dim str As String = "16461了工有6149夺中46146好31"
Label1.Text = Mid(str, str.Length - 3, 3) '取字符串最后3位数
Label2.Text = Mid(str, 1, 3) '取字符串前面3位数
相关问答
Q1: VB.net 如何获得中文字符串的长度?
上面思路是正确的, 用ascw 函数也可以
private function LenC( ps as string ) as Integer
Dim n As Integer
Dim StrLen As Integer
For n = 1 To Len(Text1.Text)
If Ascw(Mid(Text1.Text, n, 1)) 256 Then
StrLen = StrLen + 2
Else
StrLen = StrLen + 1
Next n
return strLen
end function
Q2: VB.Net如何测量含有中文的字符串长度
遍历每一个字符,看是全角还是半角的。半角的字符总数 +1,全角的字符总数 +2。
参考:
Public Function GetByteLength(ByVal value As String) As Long
Dim i As Long = 0
For Each c As Char In value
If (c.ToString().Length = System.Text.Encoding.Default.GetByteCount(value.ToString())) Then
i = i + 1
End If
i = i + 1
Next
Return i
End Function






