【问题标题】:Data from server split into a listbox来自服务器的数据拆分为列表框
【发布时间】:2012-06-26 09:24:40
【问题描述】:

来自服务器的数据需要拆分成ListBox。下面是我的代码。

private void button1_Click_2(object sender, EventArgs e)
        {
            //String[] arr = new String[1];
            listBox1.Items.Clear();

            listBox1.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());
            for (int i=0; i <= _server.Q.NoOfItem - 1; i++)
            {
                listBox1.Items.Add( _server.Q.ElementAtBuffer(i).ToString());               
            }

            listBox2.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());
            for (int i = 0; i <= _server.Q.NoOfItem - 1; i++)
            {
                String words = _server.Q.ElementAtBuffer(i).ToString();               
                listBox2.Items.Add(words.Split(new char[] { '[' , ']', ' '}));                
            }

listBox1 应该显示从服务器检索到的所有数据。 listBox2 应该显示已拆分的数据。

如何做到这一点?

【问题讨论】:

  • 您当前的代码发生了什么?你想要什么输出?
  • 我的输出:name[asd] id[123] age[12]。它假设在 listbox1 中打印 .. asd,在 listbox2 中打印 123,在 listbox3 中打印 12 ..但是在我的代码中,我将所有结果放在 listbox1 中进行检查。所以,在 listbox2 中假设打印出 asd

标签: c# split


【解决方案1】:

这应该可行:

    private void button1_Click_2(object sender, EventArgs e)
    {
        //String[] arr = new String[1];
        listBox1.Items.Clear();

        listBox1.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());
        for (int i=0; i <= _server.Q.NoOfItem - 1; i++)
        {
            listBox1.Items.Add( _server.Q.ElementAtBuffer(i).ToString());               
        }

        String words = _server.Q.ElementAtBuffer(i).ToString();  
        listBox2.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());
        listBox2.Items.AddRange(words.Split(new char[] { '[' , ']', ' '}));           
    }     

【讨论】:

  • 它仍然无法正常工作..它似乎因为它没有进行任何拆分..但我尝试放入消息框。它确实......只是不在列表框中。但我确实需要列表框。请帮帮我
  • 我用静态数据试试这个代码.. String words = "qwe rty dfg";它确实有效。但是当在服务器中拆分时,它不会
  • 你能给我一个例子来说明你的服务器数据是什么样的吗?仅当字符串中有 '[', ']' 或/和 ' ' 字符时才会拆分。
【解决方案2】:
    listBox1.Items.Clear();

    listBox1.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());
    listBox2.Items.Add("No Of Items=" + _server.Q.NoOfItem.ToString());

    for (int i = 0; i <= _server.Q.NoOfItem - 1; i++)
    {
        listBox1.Items.Add(_server.Q.ElementAtBuffer(i).ToString());

        String words = _server.Q.ElementAtBuffer(i).ToString();
        string[] arr = words.Split(new char[] { '[', ']', ' ' });
        foreach (string word in arr)
            listBox2.Items.Add(word);
    }

【讨论】:

    【解决方案3】:

    您是否尝试过使用正则表达式:

    var pattern = @"\[(.*?)\]"; 
    var matches = Regex.Matches(words, pattern);  
    foreach (Match m in matches)
    {  
       listBox2.Items.Add(/* Add matched item */);
    }
    

    【讨论】:

      【解决方案4】:
      string[] strArray = words.Split(new char[] { '[' , ']', ' '})
      for(int x = 0; x < strArray.Count; x++)
      {listBox2.Items.Add(strArray[x]}
      

      我猜你想拆分单词并添加列表框 2

      【讨论】:

      • 我之前确实使用过这个,但是它不起作用..当我尝试放入消息框时..它可以,只是它不在列表框上。
      • 不妨使用AddRange(strArray)。
      猜你喜欢
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多