views:

7272

answers:

7

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.

A: 

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...

Ed Swangren
Interestingly, I've seen ToUpper() recommended over the use of ToLower() in this sort of scenario, because apparently ToLower() can "lose fidelity" in certain cultures - that is, two different upper-case characters translate to the same lower-case character.
Matt Hamilton
Search for "Turkey test" :)
Jon Skeet
Wow, did not know that, thanks!
Ed Swangren
In some French locales, uppercase letters don't have the diacritics, so ToUpper() may not be any better than ToLower(). I'd say use the proper tools if they're available - case-insensitive compare.
Blair Conrad
Don't use ToUpper or ToLower, and do what Jon Skeet said
Peter Gfader
+87  A: 

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);
JaredPar
I've added this to my string extensions class...
tvanfosson
Indeed does look like the best way to go. Weird that such a thing is not standard framework. Thx.
borisCallens
@boris: help make it part of the framework: vote here: https://connect.microsoft.com/VisualStudio/feedback/details/435324/the-string-contains-method-should-include-a-signature-accepting-a-systen-stringcomparison-value
Hightechrider
+1  A: 

If you want to call an extension method on strings you could do this...

public static class StringExtensions
{
    public static bool ContainsInsensitive(this string searchField, string searchTerm)
    {
        return searchField.ToLower().Contains(searchTerm.ToLower());
    }
}
flesh
This will create 2 new string objects to perform the comparison
MPritch
+5  A: 

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.

mc2thaH
A: 

Wish I could mark as duplicate: http://stackoverflow.com/questions/234591/upper-vs-lower-case

Shortcut to the accepted answer: http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Alternative solution (might work better in your case): http://stackoverflow.com/questions/234591/upper-vs-lower-case#234629

Pat
That question regards COMPARING strings of case, this one is CONTAINING strings of differing case.
Anthony Mastrean
It is exactly the existing case insensitive compare method that made me think there would be a case insensitive contain method too..
borisCallens
A: 

Try this method

     string query = string.Empty;
    query = searchString;
    string path = Server.MapPath("~/contacts.xml");
    XDocument xd = XDocument.Load(path);
    var results = (from items in xd.Elements("Company").Elements("Contact")
                   where items.Element("Name").Value.ToLowerInvariant().Contains(query.ToLowerInvariant())
                   select new
                   {
                       Id = items.Element("ID").Value,
                       Photo = (string)items.Element("photo").Value,
                       Name = (string)items.Element("Name").Value,
                       BloodGroup = (string)items.Element("Bg").Value,
                       Dob = (string)items.Element("dob").Value,
                       Anniversery = (string)items.Element("avd").Value,
                       Mobile = (string)items.Element("cnum").Value,
                       Designation = (string)items.Element("desig").Value,
                       Team = (string)items.Element("team").Value
                   });

Please vote up, if its your solution

renjucool
instead i vote it down, cause this answer has nothing to do with the question.
Oliver
@Oliver Understand first before down voting where items.Element("Name").Value.ToLowerInvariant().Contains(query.ToLowerInvariant())
renjucool
@renjucool: So go on and change your example, that it matches the question. Don't publish a bunch of code where one single line contains what you'd like to say. And if you would break it down, you would come to `"LongOne".ToLowerInvariant().Contains("one".ToLowerInvariant())` which is not as good as the already given answers (espcially the as correct marked one).
Oliver
+2  A: 

Alternate solution using Regex:

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