vb6 中英文字符串截取指定长度

读卡器介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.15.2b5f5b43w03Z4k&id=17029933847icon-default.png?t=M276https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.15.2b5f5b43w03Z4k&id=17029933847 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值