-1

Something weird is happening to me. I have to following function:

private string CategoryByName(string path) {
        path.ToLower();
        if (path.Contains("obs") || path.Contains("obst")||path.Contains("birthing")) return "Birthing";
        if (path.Contains("sbu2")||(!path.Contains("sbu3")&&path.Contains("gyn"))) return "Gynecology SBU2";
        if (path.Contains("sbu3")) return "Gynecology SBU3";
        if (path.Contains("dia") || path.Contains("sprint")||(path.Contains("strechers"))) return "Strechers";
        if (path.Contains("one_day") || path.Contains("one day") || (path.Contains("oda"))) return "One-day care";
        return "";            
    }

I go through a list of file names. Including ex. "INDO OBST.xlsx", which should return as "Birthing" but it returns as "".

  • 6
    `path = path.ToLower();` - `ToLower()` returns a new string, it does not (and cannot) change the existing string in path. – Chris Feb 21 '19 at 16:20
  • God. Thank you very much. – Michal Rickwood Feb 21 '19 at 16:21
  • Or write your own Contains method that uses the `String.IndexOf` overload that takes a string comparison: https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.7.2#System_String_IndexOf_System_String_System_StringComparison_ – Flydog57 Feb 21 '19 at 16:22
  • There is a more [memory efficent way to do this](https://stackoverflow.com/questions/444798/case-insensitive-containsstring) – Liam Feb 21 '19 at 16:22
  • 1
    Completely irrelevant nitpick, but in `path.Contains("obs") || path.Contains("obst")` the test for `path.Contains("obst")` is redundant, because a string containing "obst" will obviously also always contain "obs" ;-) –  Feb 21 '19 at 16:28
  • Don't use `ToLower`. It will create an unneeded temporary string. See the answer @Flydog57 below that avoids this. – Chris Dunaway Feb 21 '19 at 19:49

3 Answers3

1

Contains is case sensitive, and judging by the ToLower() you know this.
However, ToLower() does not manipulate the path variable. Instead you should re-assign it. You can do so by changing path.ToLower() to path = path.ToLower()

MX D
  • 2,453
  • 4
  • 35
  • 47
  • 1
    `ToUpperInvariant` is ment to do this: https://learn.microsoft.com/de-de/dotnet/api/system.string.toupperinvariant?view=netframework-4.7.2 – kara Feb 21 '19 at 16:21
  • @kara This would also still require you to re-assign the variable. – MX D Feb 21 '19 at 16:23
0

The ToLower method returns the adjusted string:

path = path.ToLower();
PJRobot
  • 1,306
  • 13
  • 14
0

Write an extension method that looks something like this:

public static class Extensions
{
    public static bool ContainsNoCase(this string stringToLookIn, string stringToFind)
    {
        return stringToLookIn.IndexOf(stringToFind, StringComparison.OrdinalIgnoreCase) >= 0;
    }
}

and use ContainsNoCase in place of Contains

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • And one advantage over calling `ToLower` as the other answers recommend is that it doesn't create an unnecessary temporary string.. – Chris Dunaway Feb 21 '19 at 19:48
  • Would the down-voter like to comment on his/her rationale. This works, and it doesn't require creating new string (via `ToLower` or `ToUpper`) for each comparison. It also makes for pretty clear code. Interestingly, every answer to this question has a down-vote. – Flydog57 Feb 22 '19 at 23:53