VB按字节截取字符串

内容绝大部分来自互联网,出处请百度。

全角半角皆适用

 1     Public Function bSubstring(ByVal s As String, ByVal length As Integer) As String
 2         Dim bytes As Byte() = Text.Encoding.Unicode.GetBytes(s)
 3         
 4 
 5         Dim n As Integer = 0
 6        
 7     Dim i As Integer = 0
 8         
 9      While i < bytes.GetLength(0) AndAlso n < length
10 
11             If i And 1 = 0 Then
12                 n += 1
13             Else
14                 If bytes(i) > 0 Then
15                     n += 1
16                 End If
17             End If
18             i += 1
19         End While
20 
21         If i Mod 2 = 1 Then
22             If bytes(i) > 0 Then
23                 i = i - 1
24             Else
25                 i = i + 1
26             End If
27         End If
28         Return System.Text.Encoding.Unicode.GetString(bytes, 0, i)
29     End Function

 

 

posted @ 2013-08-26 11:21  剑握在手  阅读(1612)  评论(0编辑  收藏  举报
返回顶部↑