Visual basic 2012 IgnoreCase on if textbox.text = ""

How to ignore upper and lower case letters, this is my code:

if COMMAND_TEXT.Text = "command" then
    CONSOLE.AppendText("command entered!" & Environment.NewLine)
else
     CONSOLE.AppendText("Invalid command" & Environment.NewLine)
end if

      

I'm making a simple console, but when I try to type: Command it doesn't detect anything, but when I enter: the command just executes the code I want.

Do I have to do all possible texts? Like this:

if COMMAND_TEXT.Text = "command" then
    CONSOLE.AppendText("command entered!" & Environment.NewLine)
elseif COMMAND_TEXT.Text = "Command" then
    CONSOLE.AppendText("command entered!" & Environment.NewLine)
elseif COMMAND_TEXT.Text = "COMMAND" then
    CONSOLE.AppendText("command entered!" & Environment.NewLine)
else
     CONSOLE.AppendText("Invalid command" & Environment.NewLine)
end if

      

Or is there another simpler way like in Java EqualsIgnoreCase

+3


source to share


1 answer


I would not recommend the top case or bottom shroud as others have suggested. This can cause problems in some cultures, especially Turkish ones. Use instead String.Equals(String, StringComparison)

:

If COMMAND_TEXT.Text.Equals("command", StringComparison.CurrentCultureIgnoreCase)

      



You can use InvariantCultureIgnoreCase

or OrdinalIgnoreCase

- it depends on the context.

I also highly recommend that you avoid critical names like COMMAND_TEXT

, and use more common names instead.

+8


source







All Articles