vba splite的问题,如果分隔符是数量不定的空格要怎么办

2024-12-03 22:48:45
推荐回答(1个)
回答1:

Function M(Str)
Dim Arr
Dim Reg As Object
Set Reg = CreateObject("Vbscript.Regexp")
With Reg
.Global = True
.Pattern = "\ +"
If .test(Str) Then
M = .Replace(Trim(Str), "|")
Else
M = ""
End If
End With
Set Reg = Nothing
End Function
Function M2(Str)
Dim Arr, MyStr As String
Arr = Split(Str)
For i = 0 To UBound(Arr)
If Len(Arr(i)) <> 0 Then
MyStr = MyStr & "|" & Arr(i)
End If
Next
M2 = Right(MyStr, Len(MyStr) - 1)
End Function
Sub My()
A = "/db2log root system 2755"
MsgBox M(A)
MsgBox M2(A)
End Sub