如何进行不区分大小写的字符串比较?
如何使下面的行不区分大小写?
drUser["Enrolled"] =
(enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1);
今天早些时候,我得到了一些建议,建议我使用:
x.Username.Equals((string)drUser["Username"], StringComparison.OrdinalIgnoreCase)));
问题是我无法让它工作,我已经尝试了下面的行,它编译但返回错误的结果,它将注册的用户返回为未注册的用户,并将未注册的用户返回为已注册。
drUser["Enrolled"] =
(enrolledUsers.FindIndex(x => x.Username.Equals((string)drUser["Username"],
StringComparison.OrdinalIgnoreCase)));
谁能指出问题所在吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您始终可以使用函数:
.ToLower();
.ToUpper();
转换你的字符串然后比较它们......
祝你好运
you can always use functions:
.ToLower();
.ToUpper();
convert your strings and then compare them...
Good Luck
使用 StringComparison.CurrentCultureIgnoreCase 来代替怎么样?
How about using
StringComparison.CurrentCultureIgnoreCase
instead?我想您会在此链接中找到更多信息:
http://codeidol.com/community/dotnet/controlling-case-sensitivity-when-comparing-two-st/8873/
使用 String 类上的 Compare 静态方法来比较两个字符串。比较是否不区分大小写由其重载之一的第三个参数确定。例如:
caseSensitiveResult 值为 -1(表示 lowerCase“小于”upperCase),caseInsensitiveResult 值为 0(表示 lowerCase“等于”upperCase)。
I think you will find more information in this link:
http://codeidol.com/community/dotnet/controlling-case-sensitivity-when-comparing-two-st/8873/
Use the Compare static method on the String class to compare the two strings. Whether the comparison is case-insensitive is determined by the third parameter of one of its overloads. For example:
The caseSensitiveResult value is -1 (indicating that lowerCase is "less than" upperCase) and the caseInsensitiveResult is zero (indicating that lowerCase "equals" upperCase).
我想为 EqualsIgnoreCase 编写一个扩展方法
I'd like to write an extension method for EqualsIgnoreCase
您可以(尽管有争议)扩展
System.String
以提供不区分大小写的比较扩展方法:并按如下方式使用:
C# 允许您创建可以在项目中充当语法糖的扩展方法,非常有用我想说。
这不是答案,我知道这个问题已经很老了并且已经解决了,我只是想添加这些内容。
You can (although controverse) extend
System.String
to provide a case insensitive comparison extension method:and use as such:
C# allows you to create extension methods that can serve as syntax suggar in your project, quite useful I'd say.
It's not the answer and I know this question is old and solved, I just wanted to add these bits.
其他人的答案在这里完全有效,但不知何故,输入 StringComparison.OrdinalIgnoreCase 并使用 String.Compare 需要一些时间。
我编写了简单的字符串扩展方法,您可以在其中指定比较是区分大小写还是使用布尔值无大小写,在此处附加整个代码片段:
在整个比较大约缩短 10 个字符之后 - 比较:
在使用字符串扩展之前:
在使用字符串之后扩大:
Others answer are totally valid here, but somehow it takes some time to type
StringComparison.OrdinalIgnoreCase
and also usingString.Compare
.I've coded simple String extension method, where you could specify if comparison is case sensitive or case senseless with boolean, attaching whole code snippet here:
After that whole comparison shortens by 10 characters approximately - compare:
Before using String extension:
After using String extension:
您应该使用静态
String.Compare
函数,如下所示You should use static
String.Compare
function like following请使用此进行比较:
Please use this for comparison:
这不是 .NET Framework(4 和 +)中检查相等性的最佳实践
请改用以下内容
MSDN 推荐:
This is not the best practice in .NET framework (4 & +) to check equality
Use the following instead
MSDN recommends: