Antonios
Antonios

Reputation: 7

Reading an unknown string from a Text Box

I've got a text box which works as a console (in a form application).

I'd like to run a certain sub when the user types in:

broadcast blabla

the sub would broadcast the string blabla. How would the program recognize ONLY the first word?

Would something like this work?

If ConsoleInput.Text = "broadcast " & command Then
BroadcastMessage(command)
End If

Upvotes: 0

Views: 231

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460018

You can use String.Split:

Dim words As String() = ConsoleInput.Text.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
If words.Length > 1 AndAlso words(0).ToLower() = "broadcast" Then
    BroadcastMessage(words(1))
End If

Edit: If you want to broadcast all words it might be better to use String.Substring:

Dim spaceIndex = ConsoleInput.Text.IndexOf(" "c)
If spaceIndex > -1 Then
    Dim firstWord = ConsoleInput.Text.Substring(0, spaceIndex)
    If firstWord.ToLower = "broadcast" Then
        broadcast(ConsoleInput.Text.Substring(spaceIndex + 1))
    End If
End If

Upvotes: 1

Related Questions