Vetti85 Vetti85 - 1 year ago 91
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


Answer Source

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,"");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download