3

I have a set of switch-case statements, such as "Hello, how are you", "Hi, how can I help you?". If the input from the user is verbatim i.e: "Hello, how are you", the match works.

But if the user entered "Hello, How are You", the match fails.

I would like if the user's input is the same but different casing then it should match. i.e.

"Hello, how are you" == "Hello, How are You" == "HELLO, how are YOU"

How can this be accomplished?

Fred
  • 3,365
  • 4
  • 36
  • 57
user1144596
  • 2,068
  • 8
  • 36
  • 56
  • 4
    `switch` would likely be the wrong tool for the job. However you could always just ToLower() the strings. Though i feel the next question would be, how do i deal with extra spaces or punctuation – TheGeneral Jun 22 '20 at 05:07
  • I cannot lowercase the switch statements because they are shown elsewhere on the UI and hence casing should be maintained. – user1144596 Jun 22 '20 at 05:10
  • The ToLower() will be applied only for comparison. It won't modify the string to be displayed. – Jaime Jun 22 '20 at 05:15
  • If you `switch (yourValue.ToLower())`, nothing outside the switch is impacted by the ToLower - that doesn't necessarily make it a good approach though; I'm just saying that "because they are shown elsewhere on the UI" is irrelevant – Marc Gravell Jun 22 '20 at 05:17
  • Take a look at my answer to this https://stackoverflow.com/questions/2334134/how-to-make-the-c-sharp-switch-statement-use-ignorecase/51794712#51794712 – Flydog57 Jun 22 '20 at 05:20
  • https://stackoverflow.com/a/13965429/34092 – mjwills Jun 22 '20 at 05:21

4 Answers4

6

If you are using C# 7.0 or newer then you can use Pattern Matching with switch..case like below.

string a = "Hello, How are You";

switch (a)
{
    case string str when str.Equals("hello, how are you", StringComparison.InvariantCultureIgnoreCase):
        // Your code
        break;
    default:
        // default code
        break;

}
Karan
  • 12,059
  • 3
  • 24
  • 40
2

Just use ToLower on everything to do a non-case sensitive switch:

switch (myString.ToLower())
{
    case "hello, how are you":
        // do something
        break;
}
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
2

The switch handler in C# for strings is limited to exact character matching. Ultimately there are three approaches here:

  • use if, not a switch, and make use of manual string equality tests - perhaps specifying a StringComparison for case insensitivity
  • create a dictionary (perhaps static) with a case insensitive key comparer; put your expected strings in the dictionary - perhaps mapping to a private enum output, then switch on that output
  • use ToLower[Invariant] in the switch operand, and just eat the allocation

If it isn't high throughput, the last is probably fine

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    @Karan well, yes, but a `switch` expression that uses `when` for every meaningful test is really no different to a series of `if`, `else` tests - just shoe-horned into the `switch` syntax for the sake of it... – Marc Gravell Jun 22 '20 at 05:41
0

Instead of converting both strings to upper case or lower case and then comparing, you should use an equality comparison which can be made case-insensitive. For example:

String string1 = "Hello, how are you";
if (string1.Equals("Hello HOW are you", StringComparison.OrdinalIgnoreCase))
{
     ...
}
You should consider carefully exactly which rules are appropriate - ordinal, the current culture, the invariant culture, or possibly another culture entirely (e.g. using StringComparer.Create(culture, true)).
Kara Kartal
  • 249
  • 1
  • 2
  • 8