Rana
Rana

Reputation: 4611

Case insensitive replace without using regular expression in C#?

Is there a way to do case insensitive replace on a string without using regular expression in C#?

something like this

string x = "Hello";

x = x.Replace("hello", "hello world");

Upvotes: 6

Views: 3047

Answers (2)

Adriaan Stander
Adriaan Stander

Reputation: 166616

You can try something like

string str = "Hello";
string replace = "hello";
string replaceWith = "hello world";
int i = str.IndexOf(replace, StringComparison.OrdinalIgnoreCase);
int len = replace.Length;
str = str.Replace(str.Substring(i, len), replaceWith);

Have a look at String.IndexOf Method (String, StringComparison)

Upvotes: 5

Related Questions