1

If I type:

if TextBox1.Text = "Hello" Then MsgBox("Test")

I would like to know how to enable that so people could type for example "hElLo" instead of "Hello", or "hello".

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939

2 Answers2

1

You want to compare case-insensitive, use the appropriate StringComparison in String.Equals:

If String.Equals(TextBox1.Text, "Hello", StringComparison.CurrentCultureIgnoreCase) Then
    ' ... '
End If

You can also use the non-shared Equals in the same way, the difference is that it throws an exception if the first string is Nothing which is impossible in this case:

If TextBox1.Text.Equals("Hello", StringComparison.CurrentCultureIgnoreCase) Then
    ' ... '
End If
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You want to convert the whole string to lower-case and then perform the check as per se:

If TextBox1.Text.ToLower = "hello" Then
   MsgBox("Test")
End If

As pointed out by Tim Schmelter, the above code does not pass the so-called 'Turkey Test' (it's an interesting read, and something that I hadn't heard about before).

If you plan to use your code on a system with a non-ASCII standard locale, you should instead use:

If String.Equals(TextBox1.Text, "hello", StringComparison.CurrentCultureIgnoreCase) Then
   MsgBox("Test")
End If

Remember that the string to compare also must be lower-case if you must use the first code example that failed the Turkey Test.

Community
  • 1
  • 1
AStopher
  • 4,207
  • 11
  • 50
  • 75
  • Is not efficient and doesn't pass the ["turkey test"](http://stackoverflow.com/questions/796986/what-is-the-turkey-test). _"You've been hit with the "Turkish I" problem. The "I" in Turkish behaves differently than in most languages. Per the Unicode standard, our lowercase "i" becomes "İ" (U+0130 "Latin Capital Letter I With Dot Above") when it moves to uppercase. Similarly, our uppercase "I" becomes "ı" (U+0131 "Latin Small Letter Dotless I") when it moves to lowercase"_ – Tim Schmelter Dec 12 '14 at 20:51
  • @TimSchmelter Please explain how 'it is not efficient'. Nice catch about the Turkey Test, I'll update my answer. – AStopher Dec 12 '14 at 20:53
  • It's not efficient because it needs to create new strings due to string immutability. In this case it doesn't really matter. – Tim Schmelter Dec 12 '14 at 20:54
  • 1
    @TimSchmelter Edited, now my answer is similar to yours, damn. – AStopher Dec 12 '14 at 21:00
  • 1
    I'd leave the `ToLower` option for those who want a quick/easy/readable solution. This is a very common solution to a very common problem, which works in 99.99% of the cases (I actually thought 100% before reading Tim's comment). Regardless of which code you write, there will always be corner cases. Granted, in a big application, not passing a turkey test will be one of your *least* important concerns. – Victor Zakharov Dec 12 '14 at 21:11
  • @Neolisk: but everybody does it with `ToLower` for a lifetime because they've learned it that way. I'd start with the correct way which is not even less readable. – Tim Schmelter Dec 12 '14 at 21:44
  • @TimSchmelter: Well that's the point. Most other developers would expect it to be `ToLower`, and they already know what it means (case insensitive comparison). If put your way, there will be questions during code review, and then you will try to explain the turkey thing, and people would get offended, and you may even get fired for attempt to be smart. I know that's not what you intended, but it's the impression you may leave on people who "were always doing it this way", while working at a big company. There is always a perfect way, and a real way, the two not necessarily being the same. – Victor Zakharov Dec 13 '14 at 00:19
  • @TimSchmelter: I'm not trying to say your answer is incorrect, or that this answer is "better" - I actually upvoted both answers here. My point is that both approaches can be useful, depending on the situation. – Victor Zakharov Dec 13 '14 at 00:22