重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
遍历每一个字符,看是全角还是半角的。半角的字符总数 +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
上面思路是正确的, 用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
str = "hello,everybody. " 返回false,位置0。(不含汉字和汉语标点符号)
str = "hello,ererybody。 " 返回false,位置16。(最后一个句号是汉语标点符号)
先谢过大家了!
字符编码转换吗?
1.字符与gb2312(gbk的子集):
Public Function GBKEncode(ByVal sInput As String) As String
Dim ret_GBKEncode As String = ""
Dim i As Integer
Dim startIndex As Integer = 0
Dim endIndex As Integer
Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput) '字符以及字符串在vb2008中都是以unicode编码存储的
endIndex = x.Length - 1
For i = startIndex To endIndex
ret_GBKEncode = "%" Hex(x(i))
Next
Return ret_GBKEncode
End Function
'GBK解码
Public Function GBKDecode(ByVal sInput As String) As String
sInput = sInput.Replace("%", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'转化为字节码
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'将字节码转化为字符串
ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
2.Unicode字符串为UTF-8
Imports System.Text
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
'这里可以类推出好几种。