如何比较字符串时忽略大小写? [英] How to ignore case when comparing string?
本文介绍了如何比较字符串时忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用LINQ通过列表来搜索(用户在文本框输入查询)。
我想这是不区分大小写,并试图用IGNORECASE,但我不知道在哪里把它....我知道我可以使用上限或下限,但我想听到的话,任何人有任何的替代方法?什么会被认为是最好的做法? ?
正则表达式似乎没有任何工作。
字符串SEARCHQUERY = tbSearchQuery.Text;
VAR东西=从Y轴
x其中x.Subject.Contains(SEARCHQUERY)
选择X; // MSDN:
解决方案
使用的的String.Compare :
y中
VAR东西=从x其中的String.Compare(x.Subject,SEARCHQUERY,真)GT; = 0
选择X;
这也可以处理有下述情形之一的字符串是空的情况。
I am using linq to search through a list (user enters query in a textbox).
I want this to be case-insensitive and tried to use IgnoreCase, but I have no idea where to put it.... I know I could use upper or lower, but I would like to hear if anybody has any alternative methods? What would be considered best practise? Regex did not seem to work either?
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.Contains(searchQuery)
select x;
解决方案
Use string.Compare:
var something= from x in y
where string.Compare(x.Subject, searchQuery, true) >= 0
select x;
This can also handle situations where either strings are null.
这篇关于如何比较字符串时忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文