Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StartsWith with an array of string?

Tags:

linq

Suppose I have an array of strings:

var array = new string[] {"A", "B"}.

Then I want check if the following string: "boca" starts with the letter included in the array.

What I did is:

var result = "boca".StartsWith(array);

but the method doesn't not accept an arra as argument but a single string

like image 720
Spartaok Avatar asked Dec 06 '25 22:12

Spartaok


1 Answers

You have to loop the array and check if the word starts with anything in the array. Something like this:

var result = array.Any(s => "boca".StartsWith(s));

Assuming your array is {"A", "B"}, then result will be false, because StartsWith is case-sensitive by default.

If you want it case-insensitive, then this will work:

var result = array.Any(s => "boca".StartsWith(s, StringComparison.CurrentCultureIgnoreCase));

In this case, result will be true.

like image 50
Gabriel Luci Avatar answered Dec 08 '25 21:12

Gabriel Luci