简体   繁体   中英

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?

您可以使用Enumberable.Any记录的方法在这里可以快速检查是否存在超出您的阵列中20个字符的项目。

array.Any(x => x.Length > 20)
foreach (var s in strings)
{
    if (s.Length > 20)
    {
        // found a string with length over 20 characters
    }
}
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 ?
  }
}

Try,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM