Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自服务器的数据拆分为一个列表框_C#_Split - Fatal编程技术网

C# 来自服务器的数据拆分为一个列表框

C# 来自服务器的数据拆分为一个列表框,c#,split,C#,Split,来自服务器的数据需要拆分为一个列表框。下面是我的代码 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

来自服务器的数据需要拆分为一个
列表框
。下面是我的代码

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[] { '[' , ']', ' '}));                
            }
private void按钮1\u单击2(对象发送方,事件参数e)
{
//字符串[]arr=新字符串[1];
listBox1.Items.Clear();
listBox1.Items.Add(“Items的数量=“+\u server.Q.NoOfItem.ToString());
对于(int i=0;i这应该可以:

    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[] { '[' , ']', ' '}));           
    }     
private void按钮1\u单击2(对象发送方,事件参数e)
{
//字符串[]arr=新字符串[1];
listBox1.Items.Clear();
listBox1.Items.Add(“Items的数量=“+\u server.Q.NoOfItem.ToString());
对于(int i=0;i
string[]strArray=words.Split(新字符[]{'[',']',']','})
对于(int x=0;x
我想您应该拆分单词并添加ListBoxs2

listBox1.Items.Clear();
    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);
    }
listBox1.Items.Add(“Items的数量=“+\u server.Q.NoOfItem.ToString()); listBox2.Items.Add(“Items的数量=“+\u server.Q.NoOfItem.ToString());
对于(int i=0;i您是否尝试过使用正则表达式:

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

您当前的代码发生了什么?您想要什么输出?我的输出:name[asd]id[123]age[12].它假设打印..listbox1中的asd,listbox2中的123,listbox3中的12..但在我的代码中,我将所有结果都放在listbox1中进行检查。因此,在listbox2中,假设打印输出asdit仍然不起作用..它似乎没有进行任何拆分..但我尝试将其放在消息框中。它有..只放在列表框中。但我确实需要列表框。请他这样做lp mei使用静态数据尝试此代码..String words=“qwe rty dfg”,它确实可以工作。但是,当在服务器中执行拆分时,它不会工作。您可以给我一个示例,说明您从服务器获得的数据是什么样子的吗?只有在存在“[”,“]时,它才会被拆分字符串中的“”或/和“”字符。我以前使用过,但它不起作用..当我尝试放入消息框..它会,只是它不在列表框中。最好使用AddRange(strArray)。
var pattern = @"\[(.*?)\]"; 
var matches = Regex.Matches(words, pattern);  
foreach (Match m in matches)
{  
   listBox2.Items.Add(/* Add matched item */);
}