Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive replace without using regular expression in C#?

Tags:

c#

c#-4.0

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");
like image 326
Rana Avatar asked Oct 22 '10 04:10

Rana


1 Answers

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)

like image 86
Adriaan Stander Avatar answered Sep 30 '22 14:09

Adriaan Stander