VB6:用多个多字符分隔符分割?

发布于 2024-11-16 12:34:21 字数 350 浏览 1 评论 0原文

我目前的 split 功能有问题。我可以仅用 1 个分隔符进行分割 (split()),也可以用许多单个字符进行分割 (custom())。有办法分割这个吗?请记住,这些分隔符是不按顺序排列的。

 "MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS"

我需要你的帮助才能得到以下结果,

 "MY" , "DATA" , "IS" , "LOCATED" , "HERE" , "IN" , "BETWEEN","THE", "ATS" , "AND", "MARKS"

谢谢

I have a problem with the split function I have currently. I am able to either split with 1 delimited only (split()) or split with many single characters (custom()). Is there a way to split this? Keep in mind that these delimiters are not in order.

 "MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS"

I need your help to get the following result

 "MY" , "DATA" , "IS" , "LOCATED" , "HERE" , "IN" , "BETWEEN","THE", "ATS" , "AND", "MARKS"

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-11-23 12:34:21

创建一个新的 VB6 EXE 项目并向您提供的表单中添加一个按钮,并对 Button1_Click 事件使用以下代码:

Private Sub Command1_Click()
    Dim myText As String
    Dim myArray() As String
    Dim InBetweenAWord As Boolean
    Dim tmpString As String
    Dim CurrentCount As Integer

    CurrentCount = 0

    myText = "MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS"

    For i = 1 To Len(myText)
        If (Mid(myText, i, 1) = "@" Or Mid(myText, i, 1) = "!") And InBetweenAWord = True Then
            CurrentCount = CurrentCount + 1
            ReDim Preserve myArray(CurrentCount)
            myArray(CurrentCount) = tmpString
            tmpString = ""
            InBetweenAWord = False
        Else
            If (Mid(myText, i, 1) <> "@" And Mid(myText, i, 1) <> "!") Then
                tmpString = tmpString & Mid(myText, i, 1)
                InBetweenAWord = True
            End If

        End If
    Next

    For i = 1 To CurrentCount
        MsgBox myArray(i)       'This will iterate through all of your words
    Next


End Sub

请注意,一旦第一个 For-Next 循环完成,[myArray] 将包含您的所有单词没有不需要的字符,因此您可以在任何您喜欢的地方使用它们。我只是将它们作为 MsgBox 显示给用户,以确保我的代码有效。

Create a new VB6 EXE project and add a button to the form you will be given, and use the following code for the Button1_Click event:

Private Sub Command1_Click()
    Dim myText As String
    Dim myArray() As String
    Dim InBetweenAWord As Boolean
    Dim tmpString As String
    Dim CurrentCount As Integer

    CurrentCount = 0

    myText = "MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS"

    For i = 1 To Len(myText)
        If (Mid(myText, i, 1) = "@" Or Mid(myText, i, 1) = "!") And InBetweenAWord = True Then
            CurrentCount = CurrentCount + 1
            ReDim Preserve myArray(CurrentCount)
            myArray(CurrentCount) = tmpString
            tmpString = ""
            InBetweenAWord = False
        Else
            If (Mid(myText, i, 1) <> "@" And Mid(myText, i, 1) <> "!") Then
                tmpString = tmpString & Mid(myText, i, 1)
                InBetweenAWord = True
            End If

        End If
    Next

    For i = 1 To CurrentCount
        MsgBox myArray(i)       'This will iterate through all of your words
    Next


End Sub

Notice that once the first For-Next loop is finished, the [myArray] will contain all of your words without the un-desired characters, so you can use them anywhere you like. I just displayed them as MsgBox to the user to make sure my code worked.

云淡月浅 2024-11-23 12:34:21

VB6 中的字符处理确实很尴尬。我更喜欢使用像这样的内置函数

Private Function MultiSplit(ByVal sText As String, vDelims As Variant) As Variant
    Const LNG_PRIVATE   As Long = &HE1B6 '-- U+E000 to U+F8FF - Private Use Area (PUA)
    Dim vElem           As Variant

    For Each vElem In vDelims
        sText = Replace(sText, vElem, ChrW$(LNG_PRIVATE))
    Next
    MultiSplit = Split(sText, ChrW$(LNG_PRIVATE))
End Function

使用 MultiSplit 像这样

Private Sub Command1_Click()
    Dim vElem       As Variant

    For Each vElem In MultiSplit("MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS", Array("!!", "@@"))
        Debug.Print vElem
    Next
End Sub

Character handling is really awkward in VB6. I would prefer using built-in functions like this

Private Function MultiSplit(ByVal sText As String, vDelims As Variant) As Variant
    Const LNG_PRIVATE   As Long = &HE1B6 '-- U+E000 to U+F8FF - Private Use Area (PUA)
    Dim vElem           As Variant

    For Each vElem In vDelims
        sText = Replace(sText, vElem, ChrW$(LNG_PRIVATE))
    Next
    MultiSplit = Split(sText, ChrW$(LNG_PRIVATE))
End Function

Use MultiSplit like this

Private Sub Command1_Click()
    Dim vElem       As Variant

    For Each vElem In MultiSplit("MY!!DATA@@IS!!LOCATED@@HERE!!IN!!BETWEEN@@THE@@ATS!!AND!!MARKS", Array("!!", "@@"))
        Debug.Print vElem
    Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文