VB如何分割文本

发布网友

我来回答

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," ")

但不怎么方便,比如连续多空格,会生成多个空数组元素。所以用正规表达式对象处理较灵活,分隔符是制表符也行,而且速度也快。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com