你如何替换字符串中的第一个特定字符留下第二个字符相同

问题描述:

我只需要知道如何更换出现在不同的字符的字符串的第一个特定字符。你如何替换字符串中的第一个特定字符留下第二个字符相同

例如,我需要能够改变“需要”到“noed”,留下了第二个“e”一样。

我已经把现在正在改变“需要”到“nood”

如果您需要任何澄清,请你只问我!非常感谢!

Dim findWhat As String = "ee" 
    Dim searchThis As String = "need" 
    Dim replaceWith As String = "o" 
    Dim result As String = searchThis.Replace(findWhat, replaceWith & findWhat.Substring(1)) 
    Console.WriteLine(result) 

使用IndexOf()找到“e”的位置。现在Insert()该位置的“o”和Remove()紧随其后的位置去掉“e”的位置:

Dim word As String = "need" 
    Dim oldLetter As String = "e" 
    Dim newLetter As String = "o" 
    Dim index As Integer = word.IndexOf(oldLetter) 
    If index <> -1 Then 
     word = word.Insert(index, newLetter).Remove(index + 1, 1) 
    End If