'用api函数CopyMemory实现,没加错误处理,自己写吧 Option Explicit Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click() Dim s As String s = "中国有1000吨M型大米" Dim mybyte() As Byte MsgBox mymid(s, 1, 4)
End Sub Private Function mymid(ByVal str As String, start As Long, Length As Long) As String On Error Resume Next Dim mybyte() As Byte mybyte = StrConv(str, vbFromUnicode) Dim temparr() As Byte ReDim temparr(Length - 1) CopyMemory temparr(0), mybyte(start - 1), Length mymid = StrConv(temparr, vbUnicode) End Function
上例用了CopyMemory快速的给数组赋值,你也可以利用循环实现对数组temparr的填充
单用midb是不行的,不过可以这样:Private Sub Command1_Click() Dim s As String s = "中国有1000吨M型大米" s = StrConv(s, vbFromUnicode) Dim mystr As String mystr = StrConv(MidB(s, 1, 8), vbUnicode) MsgBox mystr
End Sub
楼上的想法是对得 但是你搞反了 。)Private Sub Command1_Click() Dim s As String s = "中国有1000吨M型大米" s = StrConv(s, vbUnicode) Dim mystr As String mystr = StrConv(MidB(s, 1, 8), vbFromUnicode) MsgBox mystr End Sub
给个现成的函数给你吧Public Function MidBA(ByVal sAscii As String, iStartPlace As Integer, Optional iLength As Integer = -1) As String Dim sTmp As String sAscii = StrConv(sAscii, vbFromUnicode) If iLength <> -1 Then sTmp = MidB(sAscii, iStartPlace, iLength) Else sTmp = MidB(sAscii, iStartPlace) End If sTmp = StrConv(sTmp, vbUnicode) sTmp = OutTAB_SC(sTmp) If iLength <> -1 Then sTmp = LimitStringLength(sTmp, iLength) End If MidBA = sTmp End FunctionPublic Function LimitStringLength(ByVal sStr As String, iLength As Integer) As String Dim I As Integer sStr = StrConv(sStr, vbFromUnicode) I = LenB(sStr) If I > iLength Then sStr = LeftB(sStr, iLength) End If sStr = StrConv(sStr, vbUnicode) If I < iLength Then sStr = sStr & Space(iLength - I) End If LimitStringLength = sStr End Function
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()
Dim s As String
s = "中国有1000吨M型大米"
Dim mybyte() As Byte
MsgBox mymid(s, 1, 4)
End Sub
Private Function mymid(ByVal str As String, start As Long, Length As Long) As String
On Error Resume Next
Dim mybyte() As Byte
mybyte = StrConv(str, vbFromUnicode)
Dim temparr() As Byte
ReDim temparr(Length - 1)
CopyMemory temparr(0), mybyte(start - 1), Length
mymid = StrConv(temparr, vbUnicode)
End Function
Dim s As String
s = "中国有1000吨M型大米"
s = StrConv(s, vbFromUnicode)
Dim mystr As String
mystr = StrConv(MidB(s, 1, 8), vbUnicode)
MsgBox mystr
End Sub
但是你搞反了 。)Private Sub Command1_Click()
Dim s As String
s = "中国有1000吨M型大米"
s = StrConv(s, vbUnicode)
Dim mystr As String
mystr = StrConv(MidB(s, 1, 8), vbFromUnicode)
MsgBox mystr
End Sub
Dim sTmp As String
sAscii = StrConv(sAscii, vbFromUnicode)
If iLength <> -1 Then
sTmp = MidB(sAscii, iStartPlace, iLength)
Else
sTmp = MidB(sAscii, iStartPlace)
End If
sTmp = StrConv(sTmp, vbUnicode)
sTmp = OutTAB_SC(sTmp)
If iLength <> -1 Then
sTmp = LimitStringLength(sTmp, iLength)
End If
MidBA = sTmp
End FunctionPublic Function LimitStringLength(ByVal sStr As String, iLength As Integer) As String
Dim I As Integer
sStr = StrConv(sStr, vbFromUnicode)
I = LenB(sStr)
If I > iLength Then
sStr = LeftB(sStr, iLength)
End If
sStr = StrConv(sStr, vbUnicode)
If I < iLength Then
sStr = sStr & Space(iLength - I)
End If
LimitStringLength = sStr
End Function