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:[email protected]";
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:[email protected]

    o/p - [email protected]

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

    o/p- Jack F. 89788987

  3. i/p- Jack F. Email:[email protected] mobile:65777 WEB

    o/p- Jack F. [email protected] 65777

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

6
  • Any problem with Regex? That is always faster and more convenient. Commented Jun 24, 2013 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. Commented Jun 24, 2013 at 11:12
  • @Kangkan: No problem with Regex. Commented Jun 24, 2013 at 11:13
  • @PLB Answers should not be put as a comment, however simple the answer is, it is an answer. Commented Jun 24, 2013 at 11:14
  • @PhilipGullick This is not a solution because it will fail in many cases, won't be helpful for future readers. Commented Jun 24, 2013 at 11:16

5 Answers 5

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:[email protected]";
Regex badWords = new Regex("email:|index|mobile:|fax:|web", RegexOptions.IgnoreCase | RegexOptions.Compiled);
myText = badWords.Replace(myText, string.Empty);
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

At beginning, I thought I would go with non-regex approach. But now, I think Regex will be much helpful. Thanks!
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:[email protected]";

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

1 Comment

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.
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

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.