【发布时间】:2019-09-05 06:53:48
【问题描述】:
我需要一些帮助,使用“;”将长字符串与文本框分开成几个不同字符串的数组。我正在编写一个简单的机器人,在这个文本框中包含机器人动作的命令,但是当填写几个不同的动作时,机器人只做其中一个。这是盒子的代码。您对我的问题有什么建议?
public void ParseCommands(string txtS)
{
char[] delimiterChars = { ';' };
string text = txtS;
System.Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiterChars);
System.Console.WriteLine($"{words.Length} words in text:");
}
private void cmdSend_Click(object sender, EventArgs e)
{
try
{
string txtS, lastMsg;
txtS = txtSend.Text;
byte[] bySend = Encoding.Default.GetBytes(txtS);
byte[] byReceive = new byte[255];
elfinSocket.Send(bySend, 0, txtS.Length, 0);
int intReceive = elfinSocket.Receive(byReceive, 0, byReceive.Length, 0);
Array.Resize(ref byReceive, intReceive);
lastMsg = Encoding.Default.GetString(byReceive);
lblRecieve.Text = lastMsg;
}
catch (SystemException error)
{
MessageBox.Show(error.Message);
}
}
【问题讨论】:
-
应该
ParseCommands不返回它的words?谁打电话给ParseCommands? -
您的问题究竟是什么?看来(从你的
ParseCommands方法)你已经知道string.Split(),所以你知道“如何将字符串分成数组”。我们不知道协议 - 如何与您的机器人交谈。所以我们不知道如何发送多个命令。 -
问题是机器人不会执行所有设置的动作,而只执行其中一个,例如left;right;up;left -> 仅向左移动。我使用的协议是 TCP/IP。我需要让它将它们识别为不同的命令。
-
那么你需要使用你的
ParseCommand方法。你写它是为了什么? ;) 并分别发送每个命令 -
是否有任何答案帮助您解决了问题?