我正在学习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
请替换成下面的格式并尝试使用
for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
{
System.Console.WriteLine("Each Line " +textBox_ReceievedData.Lines[i]);
}
prevPos = textBox_ReceievedData.Text.Split('\n');
这将为你提供一个所有文本的数组,用新的行字符分隔。\n
.
如果这是你想要的输出,那么一个 字符串 (而不是一个char数组)是你想要的。 不过 Lines() 属性已经为你提供了这个功能。
string[] prevPos = textBox_ReceievedData.Lines;
for(int i=0; i< prevPos.Length; i++)
{
System.Console.WriteLine(prevPos[i]);
}