Vetti85 Vetti85 - 2 months ago 6
C# Question

String find and replace

I have a string like

string text="~aaa~bbb~ccc~bbbddd";


The input value will be : bbb

So in the above string i should remove the value "~bbb"

The resulting string should be

text="~aaa~ccc~bbbddd";

Answer

I'm not sure what are you wanna do but if i've got it you can do this :

private string ReplaceFirstOccurrence(string Source, string Find, string Replace)
{
 int Place = Source.IndexOf(Find);
 string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
 return result;
}

var result =ReplaceFirstOccurrence(text,"~"+input,"");