Check if a string contains a word in an array

Hello, I need to check if a string contains a word that is in a array
6 Replies
P A T R I C K
P A T R I C KOP3y ago
and the best would be not to use a loop, because the string will be analyzed at each message
pip
pip3y ago
string root = "your text here";
if(root.Contains("your"))
{
//your logic here
}
string root = "your text here";
if(root.Contains("your"))
{
//your logic here
}
so in an array it would look like
string root = "this thing is the input of the otherthing's thing";
string[] stringArr = { "thing", "otherthing" };
foreach(string word in stringArr)
{
if(root.Contains(word))
{
//your logic here
}
}
string root = "this thing is the input of the otherthing's thing";
string[] stringArr = { "thing", "otherthing" };
foreach(string word in stringArr)
{
if(root.Contains(word))
{
//your logic here
}
}
M B V R K
M B V R K3y ago
you can also simplified it using LINQ like the following
string root = "this thing is the input of the otherthing's thing";
string[] stringArr = { "thing", "otherthing" };

bool result = stringArr.Any(x=>root.Contains(x));
string root = "this thing is the input of the otherthing's thing";
string[] stringArr = { "thing", "otherthing" };

bool result = stringArr.Any(x=>root.Contains(x));
but the question itself is not clear I still not understand what he want to do exactly
P A T R I C K
P A T R I C KOP3y ago
ok look
string[] blacklistedWords = new string[]
{
"world"
}

string message = "hello world"
string[] blacklistedWords = new string[]
{
"world"
}

string message = "hello world"
I would like to make a condition so as to have if the message contains the word of the array
pip
pip3y ago
both of the solutions we gave you will work
P A T R I C K
P A T R I C KOP3y ago
maybe not a condition but something to get this ok i will check ty

Did you find this page helpful?