1

Here's my code :

string zoekstring = Request.QueryString["txt"].Replace("'", "''");
    List<IntranetDocument> result = documenten.FindAll(x => x.Titel.Contains(zoekstring));

    if(result.Any())
    {
        foreach (IntranetDocument doc in result)
        {
            ListItem li = new ListItem();
            li.Text = doc.Titel;
            li.Value = doc.Locatie;
            ListFiles.Items.Add(li);
        }
    }
    else
    {
        ListItem li = new ListItem();
        li.Text = Res.Get("Algemeen_NotFound");
        li.Value = "#";
        ListFiles.Items.Add(li);
    }  

This will do a FindAll on all my documents (that I've previously loaded in via an SQL query) and checks on the document Titel. So if I search for "report" it will give me back all documents where Titel contains "report" for example for quarterly finance reports.

Now I'd also want documents returned where Titel contains "Report" with an uppercase R. Or even "rePort" or "ReporT" or "REPORT"... you get the idea. i want the FindAll ... Titel.Contains(zoekstring) to not only return the document where titel contains LITERALLY "zoekstring" but also a potential uppercase/lowercase...

How can I achieve this?

Tempuslight
  • 1,004
  • 2
  • 17
  • 34
  • why dont you convert both ToLower() and do find all. – Ravi Kanth Dec 04 '17 at 09:49
  • @RaviKanth: because there is a .NET method which is more efficient and has no issues (f.e. [with turkish `i`](https://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/)). Avoid using `ToUpper`/`ToLower` if you want to compare in a case-insensitive manner. – Tim Schmelter Dec 04 '17 at 09:55

1 Answers1

2

Use String.IndexOf with StringComparison.CurrentCultureIgnoreCase instead of Contains:

List<IntranetDocument> result = documenten.FindAll(x => x.Titel.IndexOf(zoekstring, StringComparison.CurrentCultureIgnoreCase) >= 0);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939