在字符串内的所有行中分割文本

时间:2019-02-19 10:12:19

标签: vb.net split

  • 最初,我们将从网络服务器上的文本文件中提取关键字。

将关键字作为字符串=拆分(Split(TempSMTPFile,“#关键字#”)(1),“#关键字#”)(0)

我想创建一个函数,该函数将字符串中的所有关键字分开

The keywords in this example will be: 

' This list might be changed to more or less keywords.

     Dim KeyWords As String = "[One=Test1]" & vbNewLine & "[Two=Test2]" & vbNewLine & "[Three=Test3]"

        ' I Need a function to split out all keywords and do below check between all splits.

        If KeyWords.ToLower.Contains("[" & Splited_keyword & "=") Then ' One, Two, Three
            MsgBox(Split(Split(KeyWords, "[" & Splited_keyword & "=")(1), "]")(0)) ' Test1, Test2, Test3
        End If

        ' This should print OneTest1, TwoTest2, ThreeTest3

2 个答案:

答案 0 :(得分:1)

感谢您的评论,我能够使用此解决方案解决此问题。

    Dim str As String() = keywords.Split(New [Char]() {CChar(vbCrLf)})
        For Each s As String In str

            If s.Contains("[") Then
                Dim SplittedKeyword = Split(Split(s, "[")(1), "=")(0)
                If TextUserShortDescription.Text.ToLower.Contains(SplittedKeyword.ToLower) Then
                    MsgBox(SplittedKeyword & (Split(Split(s, "[" & SplittedKeyword & "=")(1), "]")(0)))
                End If
            End If
        Next

答案 1 :(得分:0)

这可以帮助您(未试用):

Dim keyValues as String()

keyValues = KeyWords.Split(Environment.NewLine)

For Each (keyvalue as string in keyValues)
    MsgBox(keyvalue.SubString(1, keyvalue.IndexOf("["c) + " " + keyvalue.SubString(keyvalue.IndexOf("="c), keyvalue.LastIndexOf("]"c)
End For