Thursday, July 30, 2009

Can you replace one occurence in a string with C#?

I have a string, forexample aaabcc


and I want to replace only one 'a' by 'b' and not all of the a's...any ideas of how I could do it please?

Can you replace one occurence in a string with C#?
IndexOf will find a first occurrence of a string or char. Try something like:





string find = "house", replace = "home";


string text = "my house is my house";





int index = text.IndexOf( find );


text = text.Remove( index, find.Length );


text = text.Insert( index, replace );
Reply:go aabbcccd and bring it in one note earlier
Reply:Which "a" do you want to replace?





If the first one:





string str = "aaabcc";


int startIndex=str.IndexOf('a');


str=str.Remove(startIndex, 1);


str = str.Insert(startIndex, "g");





Here is a couple of good articles for string manipulation tools in C# with free source code:





http://www.mycsharpcorner.com//Post.aspx...


http://www.mycsharpcorner.com//Post.aspx...





Hope this helps,


No comments:

Post a Comment