Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search inside an array of strings

Tags:

c#

string[] words = {"januar", "februar", "marec", "april", "maj", "junij", "julij",    
"avgust", "september", "oktober", "november", "december"};

I have word "ja" for example or "dec". How can I get "januar" or "december" from an array of string? Is there any fast solution?

Thanks.

like image 393
senzacionale Avatar asked Feb 26 '23 18:02

senzacionale


1 Answers

You can use LINQ:

words.FirstOrDefault(w => w.StartsWith(str, StringComparison.OrdinalIgnoreCase))

If there was no match, this will return null.

like image 73
SLaks Avatar answered Mar 08 '23 08:03

SLaks