一个字符串如:TString="中国有1000吨M型大米"
我用 LenB得到长度是19
mid$ 能否以LenB的长度为准 ,就是mid$(TString,1,4)的结果是:中国
而不是 :中国有1如果mid$无法解决这个问题是否有其他的函数阿
谢谢

解决方案 »

  1.   

    '用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
      

  2.   

    上例用了CopyMemory快速的给数组赋值,你也可以利用循环实现对数组temparr的填充
      

  3.   

    单用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
      

  4.   

    楼上的想法是对得
    但是你搞反了 。)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
      

  5.   

    给个现成的函数给你吧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
      

  6.   

    注:MidBA函数中sTmp = OutTAB_SC(sTmp)一行对你没用:)