【问题标题】:How to separate string into an array in C#?如何在 C# 中将字符串分隔为数组?
【发布时间】: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方法。你写它是为了什么? ;) 并分别发送每个命令
  • 是否有任何答案帮助您解决了问题?

标签: c# arrays string


【解决方案1】:

看来你也应该使用你已经实现的工具。将命令字符串解构为多个部分并循环发送所有部分。您应该在ParseCommands 方法中返回数组:

public string[]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:");
    return words;
}

private void cmdSend_Click(object sender, EventArgs e)
{
    string txtS, lastMsg;
    txtS = txtSend.Text;

    string[] commands = ParseCommands(txtS);     

    foreach (var element in commands)
    {       
        byte[] bySend = Encoding.Default.GetBytes(element);
        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;
    }
}

免责声明:这似乎是一个更持久的过程,因此您不会在标签中看到收到的响应消息的更新:

lblRecieve.Text = lastMsg;

您需要使其异步并将此代码放入单独的方法中。然后等待每次响应并写入标签。

异步版本可能如下所示:

private async void cmdSend_Click(object sender, EventArgs e)
{
    string txtS, lastMsg;
    txtS = txtSend.Text;

    string[] commands = ParseCommands(txtS);     

    foreach (var element in commands)
    {   
        string response = await Task.Run(()=>
        {   
            byte[] bySend = Encoding.Default.GetBytes(element);
            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);
            return Encoding.Default.GetString(byReceive);
        });
        lblRecieve.Text = response;
    }
}

这应该可以避免 GUI 冻结

【讨论】:

  • 不应该是命令而不是foreach循环中的序列吗?
  • @Auneell 谢谢,复制粘贴错误,我懒得写foreach,所以我在LINQPad 中通过sn-p 生成它;) 纠正它
【解决方案2】:
Try This

public void ParseCommands(string txtS)
    {
        // String  
        string text = txtS;  
        // String separator  
        string[] delimiterChars = new string[] { ":" };  
        // String Split
        string[] words = text.Split(delimiterChars , StringSplitOptions.None ); 

        foreach (string firstName in words)  
        Console.WriteLine(firstName);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 2022-01-07
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多