0

I have this code that searches a list and returns the title if it matches the search word, but currently if I try searching for 'Test', it does not return the result. As in the list, is it 'test'.

Please assist.

try
{
    objCurrentWeb = SPContext.Current.Web;
    objSSList = objCurrentWeb.Lists["Sales Materials"];

    foreach (SPListItem objSSListItem in objSSList.Items)
    {
        if (Convert.ToString(objSSListItem["Title"]).Contains(searchWord))
        {
            resultLabel.Text = objSSListItem["Title"].ToString();
        }
    }
}
catch (Exception ex)
{
    resultLabel.Text = ex.Message;
}
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
Dwayne Johnson
  • 69
  • 1
  • 6
  • 18

5 Answers5

9

You can check if a string is contained in another using IndexOf, then you can specify the StringComparison method and tell it to ignore CaseSensitivity.

(Convert.ToString(objSSListItem["Title"])
    .IndexOf(searchWord, StringComparison.InvariantCultureIgnoreCase)) >= 0

To make it look better and easier to call, put it as an extension method

public static class StringExtender
{
    public static bool Contains(this string s, string str, StringComparison comparer)
    {
        return s.IndexOf(str, comparer) >= 0;
    }
}
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • 2
    This is the preferred solution I expect, but I'm not sure it is "better" looking. Wrapping this in an extension of string is probably the sexiest. I'm amused that the wrong answer has 1 vote, and mine and yours that work have -2 and 0. – Hogan Nov 06 '11 at 15:02
  • @Hogan Yep, I agree. I already had this code here, I should have posted earlier. – BrunoLM Nov 06 '11 at 15:07
4
Convert.ToString(objSSListItem["Title"]).ToUpper().Contains(searchWord.ToUpper())

or

ToLower()

both will work.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • it doesnt work as resultLabel.Text.ToUpper() gives a red underline. – Dwayne Johnson Nov 06 '11 at 05:21
  • if (Convert.ToString(objSSListItem["Title"]).ToUpper().Contains(searchWord)).ToUpper() – Dwayne Johnson Nov 06 '11 at 05:25
  • @DwayneJohnson - the code you posted above has the `ToUpper()` in the wrong place, it needs to be right after the searchWord variable not the `))` – Hogan Nov 06 '11 at 05:33
  • @Hogan but why if (Convert.ToString(objSSListItem["Title"]).Contains(searchWord, StringComparer.OrdinalIgnoreCase )) doesnt work as well? – Dwayne Johnson Nov 06 '11 at 05:37
  • Because Contains does not take two arguments... no idea what @COLDTOLD is talking about http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx – Hogan Nov 06 '11 at 05:41
  • If I'm not mistaken `ToLower` will not work with some languages. – BrunoLM Apr 04 '14 at 09:48
  • @BrunoLM - I don't understand, the question is not about all programming languages, it is about C# – Hogan Apr 04 '14 at 11:58
3

another option is using Regular expressions

Regex pattern = new Regex("Arch",RegexOptions.IgnoreCase);
if (pattern.IsMatch("string to search"))
    Console.WriteLine("Match found");
Loman
  • 989
  • 6
  • 10
0
 if (Convert.ToString(objSSListItem["Title"]).Contains(searchWord, StringComparer.OrdinalIgnoreCase ))

try it, it should ignore the case

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • 1
    Contains only takes one arguement http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx – Hogan Nov 06 '11 at 05:41
  • This code is invalid, unless you have an extension method that you didn't post. – BrunoLM Nov 06 '11 at 15:10
  • yes you might add the following extension in case it not working at all for you to modify contains `public static bool Contains(this string source, string toCheck, StringComparison comp) { return source.IndexOf(toCheck, comp) >= 0; } ` reference from here http://stackoverflow.com/questions/444798/case-insensitive-containsstring – COLD TOLD Nov 06 '11 at 19:08
0

You may write your own comparer! (Just implement IComparer interface!)

More ways to do it?

Anton
  • 1,409
  • 1
  • 19
  • 37