我读取文件得到一个字符串,里面的数字很多。比如“0.0  1.0  2.0  3.0.。”大概有十几万个,我知道数据的总个数。比如是510*499个
我是先用instr判断空格位置,然后left取,循环这样取,但很慢。要怎么做比较快呢。谢谢

解决方案 »

  1.   

    Dim strNum() As String, i As LongstrNum = Split(strData, Space(1)) '关键是这句,以后按序号读即可for i = 0 To Ubound(strNum)
        Debug.Print strNum(i),
    Next i
      

  2.   

    谢谢楼上的,刚才没说清楚,我字符串中的空格数可能是不固定的。我用我的方法每次都trim将字符串两头去空的。
      

  3.   

    '先
    L1=Len(str)
    do
     str=Replace(str,"  "," ")
     L2=Len(str)
     if L1=L2 Then Exit do
     L1=L2
    Loop
    '再
    s=Split(str," ")
      

  4.   

    把多个空格全部替换为一个空格。用正则方式替换一下吧。引用 Microsoft VBScript Regular Expressions 1.0 或 5.5    Dim strTemp As String, strNum() As String
        Dim strSource As String
        Dim re As New RegExp
        
        re.Global = True
        re.Pattern = "\s{2,}"
        
        strTemp = re.Replace(strSource, Space(1))
        strNum = Split(strTemp, Space(1))
    如果不想用正则表达式,可以循环替换,慢一些:Do Until InStr(strData, Space(2)) = 0
        strData = Replace(strData, Space(2), Space(1))
    Loop
      

  5.   

    '先
    L1=Len(str)
    do
     str=Replace(str,"  "," ")
     L2=Len(str)
     if L1=L2 Then Exit do
     L1=L2
    Loop
    '再
    s=Split(str," ")
      

  6.   

    既然说到正则表达式了,也可以用正则表达式直接提取数字:    Dim re As New RegExp
        Dim strSource As String
        Dim i As Long
        Dim strNums() As String
        Dim Matches, Match
        
        strSource = " 1.0  2.0 3.0  4.0, 5.0 6 xyz"
        re.Global = True
        re.Pattern = "\d+(\.(\d+))?"
        
        Set Matches = re.Execute(strSource)
        
        If Matches.Count > 0 Then
            ReDim strNums(Matches.Count - 1)
            i = 0
            For Each Match In Matches
                Debug.Print Match.Value & "|";
                strNums(i) = Match.Value
                i = i + 1
            Next
            Debug.Print
        End If================================================================================1.0|2.0|3.0|4.0|5.0|6|