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"
.
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"
.
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
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.