发布网友
共1个回答
热心网友
Private Sub Command1_Click()
Dim Text As String
Text = Text1.Text
'Text = "11 22 33 44 dd 345 3 56 "
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.Pattern = "[^\s]+"
Dim Matches As Object
Set Matches = regEx.Execute(Text) ' 执行搜索。
Dim Match As Object
For Each Match In Matches ' 遍历匹配集合。
Text2.Text = Text2.Text & Match.FirstIndex + 1 & vbTab & Match.Value & vbCrLf
Next
End Sub
也有个分隔为数组的函数split
比如:
dim a
Dim str As String
str = "11 22 33 44 dd 345 3 56 "
a=split(str," ")
但不怎么方便,比如连续多空格,会生成多个空数组元素。所以用正规表达式对象处理较灵活,分隔符是制表符也行,而且速度也快。