我有一个函数,它应该将一个字符串拆分为%。示例字符串:“免赔额后20%免赔额40%免赔额30%免赔额”
应分为:“免赔额20%”、“免赔额40%”、“免赔额30%”
Public Sub SplitPercentOnly(ByVal strText As String, ByVal lngCount3 As Long)
Dim i As Integer
Dim strTemps As String
Dim lngPos As Long
Dim lngChar As Long
Dim intCell As Integer
Dim strSplit() As String
strTemps = ""
intCell = 2
lngPos = 1
lngChar = 0
strSplit = Split(strText, " ")
For i = LBound(strSplit) To UBound(strSplit)
If i = LBound(strSplit) Then
strTemps = strTemps & " " & strSplit(i)
ElseIf IsNumeric(Left(strSplit(i), 1)) = True And i <> LBound(strSplit) Then
'wksMain.Cells(lngCount3, intCell).Value = Trim(strTemps)
strTemps = ""
strTemps = strSplit(i)
intCell = intCell + 1
ElseIf i = UBound(strSplit) Then
strTemps = strTemps & " " & strSplit(i)
wksMain.Cells(lngCount3, intCell).Value = Trim(strTemps)
Else
strTemps = strTemps & " " & strSplit(i)
End If
Next i
End Sub它成功地拆分了它,但有一个额外的空字符串。拆分为4个字符串,包含n个空字符串,不包含3个字符串:“免赔额后20%”、“免赔额后40%”、“免赔额后30%”
发布于 2019-07-26 03:14:15
这是一个函数,它返回一个变量数组,然后你可以对它做任何你想做的事情。
Public Function SplitPercentOnly(ByVal strText As String) As Variant
Dim re As RegExp
Set re = New RegExp
With re
.Pattern = " (\d+%)"
.Global = True
End With
SplitPercentOnly = Split(re.Replace(strText, vbTab & "$1"), vbTab)
End Function(注意:它需要对VBScript正则表达式库的引用。)
因此,您的样例输入"20% after deductible 40% after deductible 30% after deductible"将返回数组("20% after deductible","40% after deductible","30% after deductible")。
https://stackoverflow.com/questions/57204007
复制相似问题