简体   繁体   English

我只需要减少每行的输入。 然后在C#中每个单词空间剪切每一行,但我似乎无法弄清楚2D数组是如何工作的

[英]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.

So you want array of lines and for each line an array of words: 因此,您需要行的数组,并且每行都需要一个单词数组:

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 variable will be of type IEnumerable<string[]> . lines变量的类型将为IEnumerable<string[]>

If you need Arrays: 如果需要数组:

var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                .ToArray();

lines will be string[][] linesstring[][]

UPDATE Also I think you can use property Lines of TextBox to get text splittes by lines: 更新我还认为您可以使用TextBox Lines属性按行获取文本拆分:

var chop = textBox1.Lines
                   .Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                   .ToArray();

Although I'm not certain what the exact question is, I do see a problem in the code that is at least related to the title, thus why this is an answer and not a comment. 尽管我不确定确切的问题是什么,但是我确实在代码中看到了至少与标题相关的问题,因此为什么这是答案而不是注释。

Firstly, your working with jagged arrays (arrays of arrays), and not multi-dimentional arrays. 首先,您要处理锯齿状数组(数组数组),而不是多维数组。

... Normally I would give a good description of jagged arrays, but a quick google will probably explain them better than I would, so instead I will end with this: your last line of code should be ...通常,我会对锯齿状数组给出一个很好的描述,但是快速的Google可能会比我更好地解释它们,所以我将以此结尾:您的最后一行代码应为

Chop[x] = Line[x].Split(' ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM