Skip to content

Using C# to check if string contains a string in string array

Pieced this together from stackoverflow… http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array/2912541#2912541

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };

This checks if stringToCheck contains any one of substrings from stringArray.

if(stringArray.Any(stringToCheck.Contains))

If you want to ensure that it contains all the substrings, change Any to All:

if(stringArray.All(s => stringToCheck.Contains(s)))

 

Published inC#Coding

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *