2012-08-14 69 views
0

我有一个文本框作为控制台(在表单应用程序中)。从文本框中读取一个未知字符串

我想运行某个子当用户类型:

broadcast blabla 

分将播出字符串布拉布拉。 程序如何识别第一个单词?

会这样的工作?

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

回答

1

您可以使用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 

编辑:如果你想广播所有的话可能会更好使用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 
+0

它的伟大工程!但是,当我使用两个字或更多,它只读取第一个... – Antonios 2012-08-14 23:06:56

+0

@Antonios:编辑我的答案。 – 2012-08-14 23:27:00

+0

太棒了!但是,我将如何做到这一点找到接下来的两个字?例如:广播'ip''消息' – Antonios 2012-08-18 20:44:14