Friday, May 25, 2012

Case insensitive contains(string)


Is there a way to make the following return true?




string title = "ASTRINGTOTEST";
title.Contains("string");



There doesn't seem to be an overload that allows me to set the case sensitivity..

Currently I UPPERCASE them both, but that's just silly.



UPDATE

The sillyness I refer to is the i18n issues that come with up- and down casing.


Source: Tips4all

10 comments:

  1. You could use IndexOf() and pass StringComparison.OrdinalIgnoreCase

    string title = "STRING";
    bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;


    Even better is defining a new extension method for string

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

    string title = "STRING";
    bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);

    ReplyDelete
  2. You can use IndexOf() like this:

    string title = "STRING";

    if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
    {
    // The string exists in the original
    }


    Since 0 (zero) can be an index, you check against -1.

    ReplyDelete
  3. Alternate solution using Regex:

    bool contains = Regex.Match("StRiNG to search", "string", RegexOptions.IgnoreCase).Success;

    ReplyDelete
  4. One issue with the answer is that it will throw an exception if a string is null. You can add that as a check so it won't:

    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
    if (string.IsNullOrEmpty(toCheck) || string.IsNullOrEmpty(source))
    return true;

    return source.IndexOf(toCheck, comp) >= 0;
    }

    ReplyDelete
  5. You could always just up or downcase the strings first.

    string title = "string":
    title.ToUpper().Contains("STRING") // returns true


    Oops, just saw that last bit. A case insensitive compare would *probably* do the same anyway, and if performance is not an issue, I don't see a problem with creating uppercase copies and comparing those. I could have sworn that I once saw a case-insensitive compare once...

    ReplyDelete
  6. how about this:

    title.ToLower().Contains("string".ToLower())


    nice and simple...... :)

    ReplyDelete
  7. I know that this is not the C#, but in the framework (VB.NET) there is already such a function

    Dim str As String = "UPPERlower"
    Dim b As Boolean = InStr(str, "UpperLower")


    C# variant:

    string myString = "Hello World";
    bool contains = Microsoft.VisualBasic.Strings.InStr(myString, "world");

    ReplyDelete
  8. StringExtension class is the way forward, I've combined a couple of the posts above to give a complete code example:

    public static class StringExtensions
    {
    /// <summary>
    /// Allows case insensitive checks
    /// </summary>
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
    return source.IndexOf(toCheck, comp) >= 0;
    }
    }

    ReplyDelete
  9. How about this?

    string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);

    ReplyDelete
  10. Please try this extender in a static class and use it.

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

    ReplyDelete