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");
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");
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)
Replace will be able to replace the string. Its a different matter that you got index by ignoring the case. Is it a tested example?str = "Hello-Hello" it will return "hello world-hello world" but it's str="Hello-hello" it will return "hello world-hello"... this partial solution need to be redesign.The following links may be of help.
Is there an alternative to string.Replace that is case-insensitive?
http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx
http://www.west-wind.com/weblog/posts/60355.aspx
There is also the Strings.Replace function in the Microsoft.VisualBasic assembly.