There is a list

List<string> lst = new List<string>(){"киев","москва","токио","БЕРЛИН","тегеран"}; 

How to remove from it the elements that come after the element, in which all the characters are capitalized. Those. remove БЕРЛИН (as he has all capital letters) and тегеран

I know how to make a comparison - are not all the characters in the lines capitalized?

 lst.Where(x=>x.IsUpper()==x)... 

However, it is not entirely clear how to delete this and subsequent elements.

    2 answers 2

    Where is not very suitable here because it returns a "list" (IEnumerable<string>) results, it is better to use First :

     int index = lst.IndexOf(lst.First(x=>x.IsUpper()==x)); 

    And even better to do so

     int index = lst.FindIndex(x=>x.IsUpper()==x); 

    Since we need an index, not the list item itself. Well, delete:

     if (index > -1) { lst.RemoveRange(index, lst.Count - index); } 

      You can use the TakeWhile method - it selects all the elements until the condition works.

       List<string> lst = new List<string>() { "киев", "москва", "токио", "БЕРЛИН", "тегеран" }; lst = lst.TakeWhile(s => s.ToUpper() != s).ToList();