Search string in array

Hello everyone,

I’ve been working around this for to long.

I have this code to search for a specific string from an array.

This array gets its values from a txt file:
Each word in the txt file is separated by a new line;

public InputField enterNameField;
public TextAsset badWordsFile;
string[] badwords = badWordsFile.text.Split('\n');

With that a creat a string named “WordToSearch”, that is the text fomr the inputfield

string wordToSearch = enterNameField.text;
if (badwords.Any(wordToSearch.ToLower().Contains))
        {
            print("Word Found:  " + wordToSearch);
 
        }
        else
        {
            print("No word found");

        }

The issue is that this returns true all the time, no matter what is in the inputflied.

I already tried if (badwords.Contains(wordToSearch))

badwords.All(wordToSearch.ToLower()

but noting seems to work.

Can anyone help me identify the issue here?

Thank you

1 Like

array.Any and array.All take a LINQ Predicate as input, which you’re not doing. Either use a Predicate or just loop through the array the old fashioned way. (Google how to write a LINQ Predicate for more info)

bool naughtyWord = false;
foreach (string word in badwords)
{
    if (word == wordToSearch.ToLower())
    {
        naughtyWord = true;
    }
}
1 Like

thank you for the reply, gonna check how linq work, better learn it from the ground.

regards