Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial match from string array

Tags:

c#

linq

I've got a string array that looks like this:

string[] userFile = new string[] { "JohnDoe/23521/1", "JaneDoe/35232/4", ... };

I'm trying the following but this will only return exact matches. I want to be able to return a match if I am searching for "23521".

var stringToCheck = "23521";

if (userFile.Any(s => stringToCheck.Contains(s)))
{
    // ...
like image 676
Null Null Avatar asked Mar 19 '26 19:03

Null Null


1 Answers

Your Contains() call should be the other way round:

if (userFile.Any(s => s.Contains(stringToCheck)))

You want to check whether any string s in your userFile string array contains stringToCheck.

like image 182
BrokenGlass Avatar answered Mar 21 '26 08:03

BrokenGlass