5

I would like to replace "fWord" in the string "Input" as case insensitive.

while (FilteredWords.Any(Input.Contains))
{
    foreach (string fWord in FilteredWords)
    {
        Input = Input.Replace(fWord, "****");
    }
}

(FilteredWords is a list of strings and Input is the string to "clean") It works, however is case sensitive. How do I make fWord case insensitive at replacing?

HpTerm
  • 8,151
  • 12
  • 51
  • 67
user3395421
  • 53
  • 1
  • 1
  • 3
  • you can use [regexp.replace](http://msdn.microsoft.com/ru-ru/library/xwewhkd1(v=vs.110).aspx) – Grundy Mar 08 '14 at 07:16
  • no it's not a duplicate, because I already saw that method did not work to me (still was case sensitive).... – user3395421 Mar 08 '14 at 07:17
  • @user3395421 just stating "it didn't work" doesn't automatically make it not a duplicate. IT is recommended that you acknowledge the other question and explain **why** it didn't work. – psubsee2003 Mar 08 '14 at 09:45

1 Answers1

10

If the answer from the duplicate question does't help you, here is the code in your case (notice I removed the while loop - the condition in it is false if casing is different and also you don't really need it):

foreach (string fWord in FilteredWords)
{
    Input = Regex.Replace(Input, fWord, "****", RegexOptions.IgnoreCase);
}

For example, the code below

string fWord = "abc";
input = "AbC";
input = Regex.Replace(input, fWord, "****", RegexOptions.IgnoreCase);

produces the value ****.

Szymon
  • 42,577
  • 16
  • 96
  • 114