0

I am trying to compare a string to see if it contains a curse word. I assumed that I could do this using str.Contains("" || "") although I quickly realized I cannot use || with two strings. What would I use in place of this?

str.Contains("123" || "abc");

I expected it to see if it contains 123 or abc but the code segment does not work as it cannot compare two strings.

5
  • 4
    str.Contains("123") || str.Contains("abc"); ? Commented Jun 26, 2019 at 4:16
  • Try this - str.Contains("123" ) || str.Contains("abc" ) Commented Jun 26, 2019 at 4:17
  • @vasily.sib thank you so much for helping me. This worked. Commented Jun 26, 2019 at 4:22
  • 1
    Possible duplicate of How to check if a String contains any of some strings Commented Jun 26, 2019 at 4:38
  • Worth a look github.com/jamesmontemagno/Censored Commented Jun 26, 2019 at 5:00

4 Answers 4

2
var str = "testabc123";
var str2 = "helloworld";
var bannedWords = new List<string>
{
    "test",
    "ok",
    "123"
};
var res = bannedWords.Any(x => str.Contains(x));       //true
var res2 = bannedWords.Any(x => str2.Contains(x));     //false

You can do something like this. Create a list with the swear words, then you can check if the string contains any word in the list.

Sign up to request clarification or add additional context in comments.

Comments

0

Try -

var array = new List<String>() {"123", "abc"};

var found = array.Contains("abc");

Console.WriteLine("Contains: {0}", found);

1 Comment

Thank you, this solution allows me to cleanly add a lot more words to filter. It is very helpful, thank you so much.
0

Try the following approach

using System;
using System.Collections.Generic;

public class Program
{
    private static final List<String> curseWords = new List<String>() {"123", "abc"};

    public static void Main()
    {
        String input = "text to be checked with word abc";    

        if(isContainCurseWord(input)){
            Console.WriteLine("Input Contains atlease one curse word");
        }else{
            Console.WriteLine("input does not contain any curse words")
        }

    }

    public static bool isContainCurseWord(String text){

        for(String curse in curseWords){
            if(text.Contains(curse)){
                return true;
            }
        }
        return false;
    }   
}

Comments

0

Try -

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var input = "some random string with abc and 123";
        var words = new List<String>() {"123", "abc"};

        var foundAll = words.Any(word => input.Contains(word));

        Console.WriteLine("Contains: {0}", foundAll);
    }
}

1 Comment

@mjwills - I have edited the ans. Please include System.Linq & Use .Any method.

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.