首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按百分比拆分字符串

如何按百分比拆分字符串
EN

Stack Overflow用户
提问于 2019-07-25 22:09:39
回答 1查看 129关注 0票数 0

我有一个函数,它应该将一个字符串拆分为%。示例字符串:“免赔额后20%免赔额40%免赔额30%免赔额”

应分为:“免赔额20%”、“免赔额40%”、“免赔额30%”

代码语言:javascript
复制
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%”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-26 03:14:15

这是一个函数,它返回一个变量数组,然后你可以对它做任何你想做的事情。

代码语言:javascript
复制
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")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57204007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档