我怎么可以做一个区分大小写字符串比较? [英] How can I do a case insensitive string comparison?
本文介绍了我怎么可以做一个区分大小写字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能让下面的情况下,不区分大小写行?
How can I make the line below case insensitive?
drUser["Enrolled"] =
(enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1);
我得到了一些建议今天早些时候的建议我使用:
I was given some advice earlier today that suggested I use:
x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));
麻烦的是,我不能得到这个工作,我已经试过下面的线,这编译,但返回错误的结果,如报名返回登记用户的取消注册和取消注册用户。
the trouble is I can't get this to work, I've tried the line below, this compiles but returns the wrong results, it returns enrolled users as unenrolled and unenrolled users as enrolled.
drUser["Enrolled"] =
(enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"],
StringComparison.OrdinalIgnoreCase)));
任何人都可以指出问题?
Can anyone point out the problem?
推荐答案
这是.NET Framework中不是最好的做法(4安培+)来检查的平等
This is not the best practice in .NET framework (4 & +) to check equality
String.Compare(x.Username, (string)drUser["Username"],
StringComparison.OrdinalIgnoreCase) == 0
使用替代以下
String.Equals(x.Username, (string)drUser["Username"],
StringComparison.OrdinalIgnoreCase)
- 使用String.Equals方法的重载测试两个字符串是否相等。
- 使用的String.Compare 和 String.CompareTo 方法为字符串,排序的不检查平等
- Use an overload of the String.Equals method to test whether two strings are equal.
- Use the String.Compare and String.CompareTo methods to sort strings, not to check for equality.
这篇关于我怎么可以做一个区分大小写字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文