Hello everyone,

in the book that i am using to learn
c# and how to programm, i have to build this little application, what it does is quite obvious, it replace the "r" by "w" in all of the sentence. When i ran the app, i noticed that the capital R were not replaced (i understand, r and R are not the samething so, i added the line:

Code Snippet
sentence = sentence.Replace("R", "W");

So what i would like to know is:

Is there another way to do it without the extra line

Code Snippet
static void Main(string[] args)
string sentence;

Console.Write("Type a sentence ");
sentence = Console.ReadLine();
sentence = sentence.Replace("R", "W");
Console.WriteLine(sentence.Replace("r", "w"));



Re: .NET Base Class Library String.Replace more then one letter ?

Marcelo Guerra - MSFT


I think the answer is no, some methods allow you to ignore casing, like the equales method in:

string s1 = "aAbB";

string s2 = "aabb";

bool eq = s1.Equals(s2, StringComparison.OrdinalIgnoreCase);

but Replace is not one of those, so you need to use replace twice, your code may look nicer if you do it on the same line like:

sentence = sentence.Replace("R", "W").Replace("r", "w");


Hope this helps!


Re: .NET Base Class Library String.Replace more then one letter ?


Great exactly what i was looking for.

It makes it much cleaner as you said, and it almost look at it is the same command Smile

Thank you very much !

Re: .NET Base Class Library String.Replace more then one letter ?



That was of great help to me.but can you please let me know how to do the same in web applications.

I'd be greatful to you if you could tell me that.

its something like this...

user is asked to upload an sentence and i have to replace the third letter in the sentence.i mean sentence, but not a single line,there may be many sentences.sentence i meanto say a group of words delimited by fullstop,not by enter.

Thank You,