简体   繁体   中英

How to extract 2D Array from C# text box with multi line strings?

I'm having trouble to extract a 2D array from the text. However, I'm able to get the 2D array if I read it from text files. Here's what I got when reading txt files and extract it to 2D array:

var array = File.ReadLines(path)
                    .Select(line => line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                    .ToArray();

Below are my codes when I tried to extract strings from textbox to 2d array, but I only able to get single dimension array. What is the equivalent of Select(line => line when using textbox?

var array = richTextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                    .ToArray();

How can I replicate the same results when extracting it from the text box?

Try this:

var array = richTextBox1.Text.Split('\n').Select(line => line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                .ToArray();

Basically, you want to split by line breaks to achieve the same effect as File.ReadLines .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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