1

I have a bad word list. If a string contains any of item/items from the bad word list, I need to remove that bad word from the string.

List<string> badWordList = new List<string> { "email:", "index", "mobile:", "fax:", "web" };

I am able to search the string but not able to remove. Please help me out...I tried below code:

string myText = "email:abc@gmail.com";
if (badWordList.Any(w => myText.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0))
{
    // How to remove
}

Below is the set of input of expected output:

  1. i/p- email:abc@gmail.com

    o/p - abc@gmail.com

  2. i/p- Jack F. Mobile:89788987

    o/p- Jack F. 89788987

  3. i/p- Jack F. Email:t@p.c mobile:65777 WEB

    o/p- Jack F. t@p.c 65777

I would prefer a non-regex approach. Thanks for your help.

skjcyber
  • 5,759
  • 12
  • 40
  • 60
  • Any problem with Regex? That is always faster and more convenient. – Kangkan Jun 24 '13 at 11:11
  • `myText = myText.Replace(badWordList.First(i => myText.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0), "");` Generally, if this is going to handle user-input, be sure that it won't. – Leri Jun 24 '13 at 11:12
  • @Kangkan: No problem with Regex. – skjcyber Jun 24 '13 at 11:13
  • @PLB Answers should not be put as a comment, however simple the answer is, it is an answer. – Philip Gullick Jun 24 '13 at 11:14
  • @PhilipGullick This is not a solution because it will fail in many cases, won't be helpful for future readers. – Leri Jun 24 '13 at 11:16
  • possible duplicate of [Remove substring from a list of strings](http://stackoverflow.com/questions/10058046/remove-substring-from-a-list-of-strings) – mbeckish Jun 24 '13 at 11:20

5 Answers5

10

You can iterate on the bad words and remove them:

foreach (string badWord in badWordList) {
    myText = myText.Replace(badWord, string.Empty);
}

If you need a case-insensitive solution you can use the overload with a StringComparison parameter:

myText = myText.Replace(badWord, string.Empty, StringComparison.OrdinalIgnoreCase);

Note: a previous version of this answer, which is quite old, and some of the comments to this question, suggested to use a Regex for a case-insensitive string replacement (see example below).
I think that now that there is the overload with the StringComparison it's much better to just use that.

string myText = "EMAIL:abc@gmail.com";
Regex badWords = new Regex("email:|index|mobile:|fax:|web", RegexOptions.IgnoreCase | RegexOptions.Compiled);
myText = badWords.Replace(myText, string.Empty);
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
4

You can remove strings by replacing them with the empty string:

foreach (var badWord in badWordList)
{
    myText = myText.Replace(badWord, "");
}

Unfortulately this is case sensitive. For case-insensitive string replace without regular expressions, see Is there a case insensitive string replace in .Net without using Regex?

You can also do it with a regular expression, in which case case-insensitive comparison comes "for free":

var regex = String.Join("|", badWordList.Select(w => Regex.Escape(w)));
var myText = Regex.replace(myText, regex, "", RegexOptions.IgnoreCase);
Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • At beginning, I thought I would go with non-regex approach. But now, I think Regex will be much helpful. Thanks! – skjcyber Jun 24 '13 at 11:20
1

Replace the instance of the 'bad word' with string.Empty: -

List<string> badWordList = new List<string> { "email", "index:", "mobile:", "fax:", "web" };
string myText = "email:abc@gmail.com";

foreach (string s in badWordList)
{
    myText = myText.Replace(s,string.Empty);
}
DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • 3
    Only thing I'd say is the `IndexOf` check is unnecessary, since `.Replace` won't replace anything if `string` given to it does not exist in the other string. – Arran Jun 24 '13 at 11:16
1
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Collections;
            namespace WindowMaker
            {
                class Program
                {

                    static void Main(string[] args)
                    {
                        System.Console.WriteLine("Enter Main string...");
                        String str = System.Console.ReadLine();
                        System.Console.WriteLine("Enter sub string...");
                        String sub = System.Console.ReadLine();
                        Boolean flag;
                        int strlen=sub.Length;
                        int inde = str.IndexOf(sub);
                        while (inde != -1)
                        {
                            inde = str.IndexOf(sub);
                            str=str.Replace(sub,"");

                        }
                        System.Console.WriteLine("Remaining string :: {0}",str);

                            Console.Read();
                    }

                }
            }

enter image description here

Manish Bhadani
  • 437
  • 6
  • 13
0

if it is case sensitive :

List<String> badWordList = new List<String> { "email:", "index", "mobile:", "fax:", "web" };
String myText = "This is a exemple of Email: deleting with index and fax and web";

badWordList.ForEach(bw => myText = myText.Replace(bw, String.Empty));
Xaruth
  • 4,034
  • 3
  • 19
  • 26