String Replace by ignoring case C#

This is a quick blog post about replacing string by ignoring case in C#. We land in some situations where we get string and we have no control over case. In such cases to replace string by ignoring its case we can use Regex.Replace method.

Use namespace: using System.Text.RegularExpressions;

Example:

string input = "hello RoHit";

//Regex.Replace
string result = Regex.Replace(input, "rohit", "abhimanyu", RegexOptions.IgnoreCase);
Console.WriteLine(result); // prints "hello abhimanyu"

//string.Replace
string result1 = input.Replace("rohit", "abhimanyu");
Console.WriteLine(result1);  // prints "hello RoHit"


Hope you like it.

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System