0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Does the RichTextBox use \n or rather Environment.NewLine as separator?
@MarkusDeibel I'm not actually sure, if anybody knows feel free to edit
@mousetail tried your method and got an error saying 'cannot convert from 'string' to 'char'. and I changed "\n" to '\n' and now it works! Thank you so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.