Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert all strings in List<string> to lower case using LINQ?

I saw a code snippet yesterday in one of the responses here on StackOverflow that intrigued me. It was something like this:

 List<string> myList = new List<string> {"aBc", "HELLO", "GoodBye"};   myList.ForEach(d=>d.ToLower()); 

I was hoping I could use it to convert all items in myList to lowercase. However, it doesn't happen... after running this, the casing in myList is unchanged.

So my question is whether there IS a way, using LINQ and Lambda expressions to easily iterate through and modify the contents of a list in a manner similar to this.

Thanks, Max

like image 349
Max Schilling Avatar asked Oct 23 '08 18:10

Max Schilling


People also ask

How do you make all strings in a lowercase list?

Use the str. lower() Function and a for Loop to Convert a List of Strings to Lowercase in Python. The str. lower() method is utilized to simply convert all uppercase characters in a given string into lowercase characters and provide the result.

How do you lowercase in C#?

In C#, ToLower() is a string method. It converts every character to lowercase (if there is a lowercase character). If a character does not have a lowercase equivalent, it remains unchanged. For example, special symbols remain unchanged.

How do you lower an entire list in Python?

Simply use the Python string lower() method to convert every element in a list of strings into lowercase. It will convert given into lowercase letters in Python.


1 Answers

Easiest approach:

myList = myList.ConvertAll(d => d.ToLower()); 

Not too much different than your example code. ForEach loops the original list whereas ConvertAll creates a new one which you need to reassign.

like image 154
Jason Bunting Avatar answered Oct 17 '22 03:10

Jason Bunting