拆分字符串,带分隔符“1。”,“2。”等

时间:2016-02-19 05:06:01

标签: excel vba vb6

我有一个字符串

“1。这应该是一个原因.2。这是一个解决方案。想要寻找更多?3。代码是234123.4.end of solution。”

我希望将它拆分为一个n数组中的“1.这应该是一个原因”然后“2.这是一个解决方案。想要寻找更多?”那么“3.code是234123”。等等,

我尝试的代码如下:

Dim myString As String
myString = "1.This should be a cause.2.this is a solution.Want to look for more?    3.code is 234123.4.end of solution."

Dim solArr As Variant
Dim i As Integer
Dim solText As String

solArr = Split(myString, ".")
For i = 2 To UBound(solArr)
    solText = Mid(myString, InStr(1, myString, i - 1 & "."), InStr(1, myString, i & ".") - 1)

Next

1 个答案:

答案 0 :(得分:2)

对于我能够想到的所有解决方案,你会遇到多次相同数字+点(并且一个是分隔符)的问题,你仍然可以使用类似的东西:

Sub test()

  Dim myString As String
  myString = "1.This should be a cause.2.this is a solution.Want to look for more?    3.code is 234123.4.end of solution."

  Dim i As Long
  i = 2

  myString = Mid(Trim(myString), 3)

  While InStr(myString, i & ".")
    myString = Replace(myString, i & ".", "///", 1, 1)
    i = i + 1
  Wend

  Dim solArr As Variant

  solArr = Split(myString, "///")

  For i = 0 To UBound(solArr)
    solArr(i) = i + 1 & ". " & Trim(solArr(i))
  Next

End Sub