简体   繁体   中英

How to trim and convert to lower and compare between a list of string and a string

List<string> list = new List<string>() { " A ", "b" , "C"};

bool status = list.Contains(input);

I get the following status when I checked in console.

Case 1: string input = "A"; // false
Case 2: string input = "B"; // false
Case 3: string input = "C"; // true
Case 4: string input = "c "; // false 
Case 5: string input = " C"; // false
Case 2: string input = "b"; // true

I want everything to be true, hence decided to trim and also convert both to lower. I however, don't know what would be the order, first trim or first convert to lower. Also, I dont know how to do it for list, please help me.

UPDATE

found answer to part of my question. I can do input.ToLower().Trim();

But is it the right order? And how to do the same for list items in this example?

list = list.ConvertAll(d => d.ToLower().Trim());

This should help you to convert all the elements in your list to lowercase and trim extra space. This will pass all the conditions where there is a lower case string and no white space. Hope this helps.

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