'先 L1=Len(str) do str=Replace(str," "," ") L2=Len(str) if L1=L2 Then Exit do L1=L2 Loop '再 s=Split(str," ")
把多个空格全部替换为一个空格。用正则方式替换一下吧。引用 Microsoft VBScript Regular Expressions 1.0 或 5.5 Dim strTemp As String, strNum() As String Dim strSource As String Dim re As New RegExp
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|
Debug.Print strNum(i),
Next i
L1=Len(str)
do
str=Replace(str," "," ")
L2=Len(str)
if L1=L2 Then Exit do
L1=L2
Loop
'再
s=Split(str," ")
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
L1=Len(str)
do
str=Replace(str," "," ")
L2=Len(str)
if L1=L2 Then Exit do
L1=L2
Loop
'再
s=Split(str," ")
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|