Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何拆分包含{hello}{world}curley括号但保留括号的字符串(vb.net)_Vb.net_Split_Curly Brackets - Fatal编程技术网

如何拆分包含{hello}{world}curley括号但保留括号的字符串(vb.net)

如何拆分包含{hello}{world}curley括号但保留括号的字符串(vb.net),vb.net,split,curly-brackets,Vb.net,Split,Curly Brackets,我试图找到一种方法来拆分一个包含2个{}{}括号的字符串,但是在拆分后,会保留括号 在word={XXXX}{XXXX}之前 之后 字(1)={XXXX} 字(2)={XXXX} 我尝试过使用split,但这总是会删除我想要保留的}{。有人帮我摆脱痛苦!!我正在使用vb.net Dim word As String = "{hello}{world}" Dim wordArr As String() = word.Split("}") 这将有助于: Dim word As String

我试图找到一种方法来拆分一个包含2个{}{}括号的字符串,但是在拆分后,会保留括号

在word={XXXX}{XXXX}之前

之后

  • 字(1)={XXXX}

  • 字(2)={XXXX}

我尝试过使用split,但这总是会删除我想要保留的}{。有人帮我摆脱痛苦!!我正在使用vb.net

Dim word As String = "{hello}{world}" 
Dim wordArr As String() = word.Split("}")
这将有助于:

Dim word As String = "{hello}{world}"
    Dim wordArr As String() = word.Split({"}"}, StringSplitOptions.RemoveEmptyEntries)
    Dim lst1 As New List(Of String)
    For Each l In wordArr
        lst1.Add(l & "}")
    Next
    wordArr = lst1.ToArray
这将有助于:

Dim word As String = "{hello}{world}"
    Dim wordArr As String() = word.Split({"}"}, StringSplitOptions.RemoveEmptyEntries)
    Dim lst1 As New List(Of String)
    For Each l In wordArr
        lst1.Add(l & "}")
    Next
    wordArr = lst1.ToArray

您也可以尝试使用正则表达式

    Dim pattern As New Regex("(?<Word1>\{\w+\})(?<Word2>\{\w+\})")
    Dim match = pattern.Match("{Hello}{World}")

    Dim word1 = match.Groups("Word1").Value
    Dim word2 = match.Groups("Word2").Value
Dim模式作为新的正则表达式((?\{\w+\})(?\{\w+\})”
Dim match=pattern.match(“{Hello}{World}”)
Dim word1=匹配.Groups(“word1”).值
Dim word2=匹配.Groups(“word2”).值

您也可以尝试使用正则表达式

    Dim pattern As New Regex("(?<Word1>\{\w+\})(?<Word2>\{\w+\})")
    Dim match = pattern.Match("{Hello}{World}")

    Dim word1 = match.Groups("Word1").Value
    Dim word2 = match.Groups("Word2").Value
Dim模式作为新的正则表达式((?\{\w+\})(?\{\w+\})”
Dim match=pattern.match(“{Hello}{World}”)
Dim word1=匹配.Groups(“word1”).值
Dim word2=匹配.Groups(“word2”).值
这里有一个Linq方法:

    Dim brace As Char = "}"c
    Dim output As String() = (From s In input.Split(brace) 
        Where s <> String.Empty 
        Select s + brace).ToArray()
Dim brace As Char=“}”c
将输出设置为字符串()=(来自input.Split(大括号)中的s)
哪里是字符串。空
选择s+大括号)。ToArray()
这里有一个Linq方法:

    Dim brace As Char = "}"c
    Dim output As String() = (From s In input.Split(brace) 
        Where s <> String.Empty 
        Select s + brace).ToArray()
Dim brace As Char=“}”c
将输出设置为字符串()=(来自input.Split(大括号)中的s)
哪里是字符串。空
选择s+大括号)。ToArray()

这里有另一个使用迭代器的选项:

Public Iterator Function SplitAfter(input As String, delim As Char) As IEnumerable(Of String)
    Dim sb As StringBuilder = New StringBuilder(input.Length)
    For Each c As Char In input
        sb.Append(c)
        If c = delim Then
            Yield sb.ToString()
            sb.Clear()
        End If
    Next
    If sb.Length > 0 Then
        Yield sb.ToString()
    End If
End Function

Dim word As String = "{hello}{world}"
Dim wordArr As String() = SplitAfter(word, "}"c).ToArray()
For Each w As String In wordArr
    Console.WriteLine(w)
Next
输出:

{hello}
{world}

这里有另一个使用迭代器的选项:

Public Iterator Function SplitAfter(input As String, delim As Char) As IEnumerable(Of String)
    Dim sb As StringBuilder = New StringBuilder(input.Length)
    For Each c As Char In input
        sb.Append(c)
        If c = delim Then
            Yield sb.ToString()
            sb.Clear()
        End If
    Next
    If sb.Length > 0 Then
        Yield sb.ToString()
    End If
End Function

Dim word As String = "{hello}{world}"
Dim wordArr As String() = SplitAfter(word, "}"c).ToArray()
For Each w As String In wordArr
    Console.WriteLine(w)
Next
输出:

{hello}
{world}

Split不删除任何内容-它简单地在这些点上拆分行/字符串。@Protoix:我想他想在返回的子字符串中保留大括号。Split不删除任何内容-它简单地在这些点上拆分行/字符串。@Protoix:我想他想在返回的子字符串中保留大括号。非常感谢BanForFun谢谢你的帮助。这对我来说太好了。谢谢BanForFun真的很感谢你的帮助。这对我来说非常有效。