将接收到的数据串水平复制到1d char数组中 c#表格中

问题描述 投票:0回答:1

我正在学习C#,并尝试使用数组,我通过一个表单应用程序从用户那里获取输入,并希望将其复制到一个名为prevPos的数组中,格式如下所示

接收数据(字符串)。

string1: "hello"
string2: "123"
       //counting how many lines and using that to determine position associated with each 
       recieved 
      char[] prevPos;
      prevPos = textBox_ReceievedData.Text.ToCharArray();

      //count how many lines of receieved data in textbox
        for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
        {

            System.Console.WriteLine("charArray "  +prevPos[i]);
        }

现在,如果我想调用它,我会得到这个, 我不希望这个fomat。

        prevPos[1]=h
        prevPos[2]=e 
        prevPos[3]=l
       etc.

我想要这个输出。

prevPos[1]=hello
prevPos[2]=123
c# arrays forms
1个回答
0
投票

请替换成下面的格式并尝试使用

        for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
        {

            System.Console.WriteLine("Each Line "  +textBox_ReceievedData.Lines[i]);
        }

0
投票
prevPos = textBox_ReceievedData.Text.Split('\n');

这将为你提供一个所有文本的数组,用新的行字符分隔。\n.


0
投票

如果这是你想要的输出,那么一个 字符串 (而不是一个char数组)是你想要的。 不过 Lines() 属性已经为你提供了这个功能。

string[] prevPos = textBox_ReceievedData.Lines;
for(int i=0; i< prevPos.Length; i++)
{
    System.Console.WriteLine(prevPos[i]);
}
© www.soinside.com 2019 - 2024. All rights reserved.