H
H
Hikky2020-08-22 19:56:54
C++ / C#
Hikky, 2020-08-22 19:56:54

How to check if a string contains a word from an array?

Hello! Can you please tell me how can I check a string for the presence of words from a certain array? I need the most optimal option)

I tried to use the String.Contains method, but (if I understand correctly) only one word can be checked there:

if (message.Contains("Хай")) //Срабатывает отлично)


PS: new to c#)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gennady Kruglov, 2020-08-22
@Hikky

You can use LINQ.

var foo = stringArray.Any(str => message.Contains(str));

M
Maxim, 2020-08-22
@XTerris

Going through the elements of the array with words (the for loop), check each of them for content in the string.

V
Vladimir Korotenko, 2020-08-22
@firedragon

Although they joke about 1C programmers, but for NLS
the program looks exactly like this
add word A
to the list add word B to the list
As long as there are values ​​\u200b\u200bin the list, assign this value to the variable
Everything that is inside is repeated
if the array contains a string from the assigned value, then we do something
End of repetition Everything inside repeats

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question