- Home /
how do I check if a string is inside of an array
I got a string array, i do not want to have a switch statement checking each individual variable in the array and comparing it to the sting. i tried to use
if (arrayVar.Contains(individualString)){
// do something
}
but i am getting some errors. if some one has any ideas or better options to do this pls let me know. thanks in advanced
Assu$$anonymous$$g you use standard .NET array (string[]), then Contains method is Linq extension and requires using System.Linq;
(or import System.Linq;
in JavaScript).
If you get errors despite including proper namespace, please post these errors.
Answer by karl_ · Feb 05, 2014 at 01:29 AM
You have to loop through the array and check each entry.
foreach(string str in array)
{
if(str.Contains(individualString))
DoSomething();
}
If your question is whether or not there's a better way to do this, you could also check out the Linq library which provides some handy functions for sorting and working with arrays.
Edit: After writing this I got a little curious as to faster/more readable solutions. This SO question has a lot of good options:
Your answer
![](https://koobas.hobune.stream/wayback/20220613132554im_/https://answers.unity.com/themes/thub/images/avi.jpg)