在字典中将字符串转换为小写. [英] Converting string to lowercase in a dictionary.

查看:214
本文介绍了在字典中将字符串转换为小写.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一本字典,其中的键和值均为字符串类型.
值以大写字母开头,例如"Test".
我必须检查此词典是否包含用户输入的字符串.用户输入的字符串可以是大小写混合的任何情况.

如何将字典值转换为小写并与转换为小写的用户输入值进行比较?



字典< string,string> str =新字典< string,string>();
str.Add("1","Test");

现在用户输入的字符串可以是"test".我将在用户输入字符串上执行ToLower().如何使用字典的contains方法?如何将字典值转换为小写并进行比较?记住我现在没有钥匙..

请建议

Hello,
I have a dictionary where both key and value are of type string.
Values are starting with capital letter like "Test".
I have to check whether this dictionary contains user input strings. User inputed strings can be of any case like mix of lower/upper case.

How do I convert dictionary values into lower case and compare against lower case converted user input values?

Like

Dictionary <string,string> str = new Dictionary<string,string>();
str.Add("1","Test");

now user input string can be "test". i will do it ToLower() on user input string. How do I use dictionary''s contains method? how do I convert dictionary values into lower case and compare it? Remember I dont have key now..

Please suggest

推荐答案

Lambda表达式...
Lambda Expressions ...
if(str.Select(x=>x.Value).Where(x=>x.ToLower()==test.ToLower()).Count()>0)
{/*Code*/}



如果您熟悉它,这应该是处理集合项的快速代码



if you are familiar with it this should be a quick code for manipulating collection items


将它们全部以大写或小写形式添加到字典中,或者将用户输入转换为正确的类型并检查.与String.Equals不同,Dictionary.ContainsValue不能重写以指定区分大小写.您可以重写比较器,但这是我要做的一些简单工作.
Either add them to your dictionary as all upper or all lower case, or convert the user input to the correct type and check. Unlike String.Equals, Dictionary.ContainsValue has no override to specify case sensitivity. You could override the comparer, but that is more work that I would do for something this simple.


创建第二个词典:
Create a second dictionary:
Dictionary<String, List<String>> reversed;


该词典中的键将是原始词典中值的小写版本.该值中的字符串列表是因为您可以在原始字典中使用多个键,但键值不同但键值不同.每当您在原始字典中插入条目时,都会对反向字典再次这样做.

从此字典中获取值时,可以遍历字符串列表以在原始字典中查找值,并检查它们是大写还是小写.


The key in this dictionary will be the lowercase version of the value from the original dictionary. The list of strings in the value is because you can have mulitple keys in the original dictionary with different keys but the same value. Whenever you insert an entry into the original dictionary, you do so again for the reversed dictionary.

When you get the value from this dictionary, you can go over the list of strings to lookup values in the original dictionary and check if they are uppercase or lowercase.


这篇关于在字典中将字符串转换为小写.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆