比较字符串时如何忽略大小写?

时间:2011-10-18 08:42:13

标签: c#

我正在使用linq搜索列表(用户在文本框中输入查询)。

我希望这个不区分大小写,并尝试使用IgnoreCase,但我不知道在哪里放它....我知道我可以使用上部或下部,但我想听听是否有人有任何替代方案方法?什么是最佳做法? 正则表达式似乎也不起作用?

        string searchQuery = tbSearchQuery.Text;
        var something= from x in y 
                       where x.Subject.Contains(searchQuery)
                       select x;

8 个答案:

答案 0 :(得分:7)

由于还没有其他人提出过,我建议使用静态String.Equals,这样您就不必担心null并只返回您想要的信息。

String.Compare也可以,但你不是要对字符串进行排序(整数返回值的原因),只是在不区分大小写的比较中确定它们是否相等。

var something = from x in y 
                where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
                select x;

答案 1 :(得分:5)

string searchQuery = tbSearchQuery.Text; 
var something= from x in y  
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
               select x; 

答案 2 :(得分:2)

我使用以下自己的扩展(对于普通字符串)

 public static bool Contains(this string source, string toCheck, StringComparison comp)
 {
    return source.IndexOf(toCheck, comp) >= 0;
 }

 public static bool ContainsIgnoreCase(this string source, string toCheck)
 {
    return source.IndexOf(toCheck, StringComparison.InvariantCultureIgnoreCase) >= 0;
 }

HTH

答案 3 :(得分:0)

尝试

string searchQuery = tbSearchQuery.Text; 
var something= from x in y  
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) != -1

答案 4 :(得分:0)

您可以改用IndexOf吗?

string searchQuery = tbSearchQuery.Text;
var something= from x in y 
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
               select x;

答案 5 :(得分:0)

如果您使用LINQ-to-SQLEntity Framework,则整理规则是固定的,并在表定义中设置。比较它们的唯一方法是ToUpper(或ToLower,但最好是ToUpper,这里写的是http://www.siao2.com/2007/10/01/5218976.aspx)这两部分比较。

答案 6 :(得分:0)

我是扩展方法的第二种方式。我有一个课我用。只需在你的项目中加入它并引用你的名字空间即可离开。

using System;

namespace YOUR NAME SPACE
{
    public static class StringExtensionMethods
    {
        /// <summary>
        /// Extention method to allow a string comparison where you can supply the comparison type 
        /// (i.e. ignore case, etc).
        /// </summary>
        /// <param name="value">The compare string.</param>
        /// <param name="comparisionType">The comparison type - enum, use OrdinalIgnoreCase to ignore case.</param>
        /// <returns>Returns true if the string is present within the original string.    </returns>
        public static bool Contains(this string original, string value, StringComparison comparisionType)
        {
            return original.IndexOf(value, comparisionType) >= 0;
        }
    }
}

以下是使用它的LINQ查询示例:


var results = from promotion in response
              where
                  String.IsNullOrEmpty(find.Code) ||
                  (promotion.Code != null && promotion.Code.Contains(find.Code, StringComparison.OrdinalIgnoreCase))
              where
                  String.IsNullOrEmpty(find.Name) ||
                  (promotion.Name != null && promotion.Name.Contains(find.Name, StringComparison.OrdinalIgnoreCase))
              select promotion;

答案 7 :(得分:-1)

使用string.Compare

    var something= from x in y 
                   where string.Compare(x.Subject, searchQuery, true) >= 0
                   select x;

这也可以处理任何字符串为空的情况。

相关问题