根据数组的索引替换实例

时间:2017-02-07 14:39:21

标签: .net regex string vb.net replace

我希望更换所有事件" [2]"在给定的字符串中 这是我目前的职能

Dim replace() As String
replace = Split("3,2,4,1", ",")
Dim orig As String = "[2] [2] [2] [2]"
Dim search As String = "[2]"
 sb = New StringBuilder(orig)
For i As Integer = 0 To replace.Length - 1

                Dim Occurrence = sb.ToString().IndexOf(search)
                If Occurrence > -1 Then
                    If (replace(j) = "2") Then
                        sb.Replace(search, "[2]", firstOccurrence, search.Length)
                     j = j + 1
                    Else
                        sb.Replace(search, "[" & replace(j) & "]", firstOccurrence, search.Length)
                        j = j + 1
                    End If
                End If

            Next

其实我明白了:

Output : [3] [4] [1] [2]

我希望得到这样的东西:

Output : [3] [2] [4] [1]

感谢' S

4 个答案:

答案 0 :(得分:1)

您必须跟踪从何处开始搜索,并且您还可以通过删除所有这些单字母索引来大大简化该循环并使其更易于维护。也就是说,你的目标可以这样完成:

    Dim orig As String = "[2] [2] [2] [2]"
    Dim searchText As String = "[2]"
    Dim replaceChars() As String = Split("3,2,4,1", ",")
    Dim startIndex As Integer = 0

    For replaceIndex As Integer = 0 To replaceChars.Count - 1
        Dim searchIndex As Integer = orig.IndexOf(searchText, startIndex) + 1
        orig = orig.Remove(searchIndex, 1).Insert(searchIndex, replaceChars(replaceIndex))
        startIndex = searchIndex
    Next

    MsgBox(orig)

请注意startindex变量,它用于告诉IndexOf从哪里开始搜索...在每个循环结束时,它会更新到找到匹配项的最后位置。这使IndexOf在下一次迭代期间不会搜索整个字符串

答案 1 :(得分:0)

由于您要更换已有的内容,因此每次都需要跟踪下一次搜索的起点

Dim Occurrence = sb.ToString().IndexOf(search, Occurance)

你已经知道了这个位置

sb.Replace(search, "[2]", Occurrence, search.Length)

sb.Replace(search, "[" & replace(j) & "]", Occurrence, search.Length)

结束
Occurance += search.Length

并且不要在这样的例程中使用搜索和替换等变量名称。它只是令人困惑....看起来像函数调用。当你在六个月内回到你的代码时,你会感谢我。

答案 2 :(得分:0)

廉价而肮脏的解决方案

    Dim Values() As String = {"3", "2", "4", "1"}
    Dim Orig As String = "[2] [2] [2] [2]"
    Dim Output As String = Replace(Orig, "[2]", "[^]")
    For Each ReplaceMent As String In Values
        Output = Replace(Output, "[^]", "[" & ReplaceMent & "]", 1, 1)
    Next
    'Put back any unused twos
    Output = Replace(Output, "[^]", "[2]")

答案 3 :(得分:-1)

要做到这一点,你可以做的一件事是将你的字符串拆分到你想要替换的子字符串上,所以这个字符串:

one[2]two[2]three[2]four[2]five

成为一个包含五个元素的数组:

{"one", "two", "three", "four", "five"}

一旦你拥有了它,你可以将它与你的替换品一起“压缩”:

  {"one",    "two",    "three",    "four",    "five"}
& {"[3]",    "[2]",    "[4]",      "[1]"}
-----------------------------------------
  {"one[3]", "two[2]", "three[4]", "four[1]", "five"}

将整个事情重新加入:

one[3]two[2]three[4]four[1]five

在代码中,看起来像这样:

Dim replace As String() = {"[3]", "[2]", "[4]", "[1]"}
Dim orig = "[2] [2] [2] [2]"
Dim search = "[2]"

Dim result = String.Concat(
    orig.Split({search}, StringSplitOptions.None).Zip(
        Enumerable.Concat(replace, {""}),
        Function (a, b) a & b
    )
)

将它分成另一个函数可能是个好主意:

<Extension>
Function ReplaceSequence(s As String, find As String, replacements As IEnumerable(Of String)) As String
    Return String.Concat(
        s.Split({find}, StringSplitOptions.None).Zip(
            Enumerable.Concat(replacements, {""}),
            Function (a, b) a & b
        )
    )
End Function

然后可以这样使用:

Dim result = "[2] [2] [2] [2]".ReplaceSequence(
    "[2]",
    {"[3]", "[2]", "[4]", "[1]"}
)