Sandeep Sandeep - 6 months ago 64
C# Question

String.Replace ignoring case

I have a string called "hello world"

I need to replace the word "world" to "csharp"

for this I use:

string.Replace("World", "csharp");

but as a result, I don't get the string replaced. The reason is case sensitiveness. The original string contains "world" whereas I'm trying to replace "World".

Is there any way to avoid this case sensitiveness in string.Replace method?


You could use a Regex and perform a case insensitive replace:

class Program
    static void Main()
        string input = "hello WoRlD";
        string result = 
           Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
        Console.WriteLine(result); // prints "hello csharp"