Function MyMidStr(ByVal InputStr As String, ByVal StrLen As Integer) As String '中英数字符串在VB中的截取指定字节字符串
Dim a() As Byte
ReDim b(StrLen) As Byte
Dim I As Integer
InputStr = Trim(InputStr) + Space(StrLen)
a = StrConv(InputStr, vbFromUnicode) '字符串转换为字节数组
For I = 0 To StrLen - 1
b(I) = a(I)
Next
MyMidStr = StrConv(b, vbUnicode)
End Function
Function MidStr(ByVal InputStr As String, ByVal StrLen As Integer) As String '中英数字符串在VB中的截取指定字节字符串
ReDim b(StrLen + 2) As Byte
Dim I, j As Integer
InputStr = Trim(InputStr) + Space(StrLen)
Dim asc1 As Double
Dim asc2 As Double
Dim longi As Double
I = 1
j = 1
While (I <= StrLen)
longi = Asc(Mid(InputStr, j, 1))
j = j + 1
If (longi < 0) Then '汉字
longi = 65536 + longi
b(I) = ((longi - (longi Mod 256)) / 256) Mod 256 '千万不要以为"这些语句太繁杂,mod多此一举"
I = I + 1
b(I) = longi Mod 256
I = I + 1
Else '英文或数字
b(I) = longi Mod 256
longi = longi / 256
I = I + 1
End If
Wend
MidStr = ""
I = 1
While (I <= StrLen)
If b(I) <= 128 Then
MidStr = MidStr + Chr(b(I))
Else
asc1 = b(I)
I = I + 1
asc2 = b(I)
If I <= StrLen Then
MidStr = MidStr + Chr(asc1 * 256 + asc2)
Else
MidStr = MidStr + " "
End If
End If
I = I + 1
Wend
End Function
Private Sub Command1_Click()
Text2.Text = MyMidStr(Text1.Text, 100)
End Sub
Private Sub Command2_Click()
Text2.Text = MidStr(Text1.Text, 100)
End Sub
vb6 中英文字符串截取指定长度
最新推荐文章于 2024-06-26 18:12:07 发布