我需要根据分隔符将字符串分成两部分,但问题是我的分隔符由多个分隔符组成。
例如:
字符串 - > 7...5
我需要提取7和5,但split函数返回与variant相同的字符串。
任何简单的方法吗?我不想使用RegExp。
Function f1(Value As String) As Boolean
Dim flag As Boolean
Dim length As Integer
Dim c As Variant
Dim c1 As Variant
flag = True
If IsNumeric(Value) = False Then
flag = False
End If
c = Split(Value, ":")
If UBound(c) < 1 Then
c = Split(Value, "...") 'Here split does not divide it into array
End If
Function f()
If f1(Cells(5,10).Value) = False Then
' Do something
End If
End Function
答案 0 :(得分:0)
方式1 如果您知道长度并且只有点,那么您可以将它们用作分隔符。例如Split("7...5","...")
实施例
Sub Sample()
Dim sDelim As String, sTest As String
sTest = "7...5"
sDelim = "..."
Debug.Print Split(sTest, sDelim)(0)
Debug.Print Split(sTest, sDelim)(1)
End Sub
方式2 如果您不知道长度并且只有点数,那么请使用此
Option Explicit
Sub Sample()
Dim sDelim As String, sTest As String
Dim i As Long, j As Long
sTest = "7...5"
'~~> other test Scenarios
'sTest = "75.....56"
'sTest = "734.......5"
'sTest = "7.......5567"
For i = 1 To Len(sTest)
If Mid(sTest, i, 1) = "." Then
Debug.Print "The First Number is " & Left(sTest, i - 1)
j = i
Exit For
End If
Next i
For i = j To Len(sTest)
If Mid(sTest, i, 1) <> "." Then
Debug.Print "The Second Number is " & _
Right(sTest, Len(sTest) - (i - 1))
Exit For
End If
Next i
End Sub
答案 1 :(得分:0)
您可以使用RegExp分割这样的字符串
Private Sub Form_Load()
Dim vSplit As Variant
vSplit = preg_split("\D+", "1..7")
Debug.Print vSplit(0), vSplit(1)
End Sub
Function preg_split(find_re, text)
Dim esc As String
Dim re As Object
Dim idx As Long
esc = ChrW(&HE1B6) '-- U+E000 to U+F8FF - Private Use Area (PUA)
Set re = CreateObject("VBScript.RegExp")
With re
.Global = True
If Left$(find_re, 1) = "/" Then
idx = InStrRev(find_re, "/")
.Pattern = Mid$(find_re, 2, idx - 2)
.IgnoreCase = (InStr(idx, find_re, "i") > 0)
.MultiLine = (InStr(idx, find_re, "m") > 0)
Else
.Pattern = find_re
End If
End With
preg_split = Split(re.Replace(text, esc), esc)
End Function
答案 2 :(得分:0)
问题在于字符串“......”。因为,它有一个预先定义的ASCII码为0133.为了解析字符串,我已经指定了字符代码并使用了Chr函数将其转换为字符。
c = Split(ValueString,Chr(0133)) 'after this c contains two arrays