[英]how to copy received data from string into 1d char array c# forms
所以我只是學習 C# 並嘗試使用 arrays,我通過 forms 應用程序從用戶那里獲取輸入,並希望將其復制到名為 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]);
}
現在,如果我想調用它,我會得到這個,我不想要這個格式:
prevPos[1]=h
prevPos[2]=e
prevPos[3]=l
etc.
我想要這個 output:
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
分隔的所有文本的數組。
如果那是您想要的 output,那么您想要的是字符串數組(不是字符數組)。 不過, Lines() 屬性已經為您提供了:
string[] prevPos = textBox_ReceievedData.Lines;
for(int i=0; i< prevPos.Length; i++)
{
System.Console.WriteLine(prevPos[i]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.