Is it possible to check if a string contains an item from an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible to check if a string contains an item from an array?

Essentially I have a program that takes user input and splits the words up and stores them individually in an array. I have another array that contains numbers and symbols. I need to search the first array for strings containing those numbers and symbols. I need to do this at least twice within the program (hence using an array for the numbers and symbols to save code). I essentially want to do this: if (s.Contains (numberSymbols[ ])){} which doesn't work. How should I do this?

7th Nov 2016, 1:30 AM
Matthew Tomlin
Matthew Tomlin - avatar
2 Answers
0
You should call a method of array. if (numberSymbols.Contains (yourstring)). It should be works, if "yourstring" is alone string variable, But by your description, I am affraid, you want to ask "if this array contains whichever string item from another array. And this is difficulty in one method. Maybe a solution should be a complex query in Linq, But the simplest way is use a loop foreach (string sItem in sArray) {if (numberSymbols.Contains (sItem)) ....}
7th Nov 2016, 2:42 AM
Petr Hatina
Petr Hatina - avatar
0
It sounds to me that each array depends on the other. If thats the case you should think of using a dictionary where you have a key, value pair. Easier to find a certain value with a given key. Correct me if i am wrong about the array dependencies.
7th Nov 2016, 11:06 AM
Ousmane Diaw