silla
silla

Reputation: 1327

Check all values in string[] for length?

I have an string[] with some values. I would like to check the length of every string. Actually I just want to be sure that none of them got a length over 20 chars. Is there a fast way?

Upvotes: 2

Views: 260

Answers (4)

Jignesh Thakker
Jignesh Thakker

Reputation: 3698

Try,

if (array.Count(o => o.Length > 20) > 0)
{
// Do something
}

Upvotes: 0

Michal Klouda
Michal Klouda

Reputation: 14521

You can use Enumberable.Any method documented here to quickly check if there is an item exceeding 20 characters in your array.

array.Any(x => x.Length > 20)

Upvotes: 18

Shyju
Shyju

Reputation: 218952

string[] youtStringArray= new string[] {"Michigan", "NewYork", "Florida"};
foreach(var item in youtStringArray)
{
  if(item.Length>20)
  {
    //do some thing , may be substring  to first 20 ?
  }
}

Upvotes: 2

dtb
dtb

Reputation: 217411

foreach (var s in strings)
{
    if (s.Length > 20)
    {
        // found a string with length over 20 characters
    }
}

Upvotes: 2

Related Questions