简体   繁体   English

如何修剪并转换为较低并在字符串列表和字符串之间进行比较

[英]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();我可以做 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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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