[英]I just need to cut an input per line. and then cut each line per word space in C# but I cant seem to figure out how 2d arrays work
string[][] Chop = null;
string[] Line = null;
private void button1_Click(object sender, EventArgs e)
{
Line = textBox1.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); // Im succesfull at cutting the input per line and stores it per line in Line variable.
for(int x = 0;x < Line.Length; x++)
Chop[][x] = Line[x].Split(' ');
//I want the Chop to have an array of array of strings.
因此,您需要行的數組,並且每行都需要一個單詞數組:
string[][] lineWords = textBox1.Text
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
.Select(l => l.Split())
.ToArray();
var lines = from line in text.Split(new [] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
select line.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
lines
變量的類型將為IEnumerable<string[]>
。
如果需要數組:
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
lines
將string[][]
更新我還認為您可以使用TextBox
Lines
屬性按行獲取文本拆分:
var chop = textBox1.Lines
.Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
盡管我不確定確切的問題是什么,但是我確實在代碼中看到了至少與標題相關的問題,因此為什么這是答案而不是注釋。
首先,您要處理鋸齒狀數組(數組數組),而不是多維數組。
...通常,我會對鋸齒狀數組給出一個很好的描述,但是快速的Google可能會比我更好地解釋它們,所以我將以此結尾:您的最后一行代碼應為
Chop[x] = Line[x].Split(' ');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.