Assuming you want to convert the Text in a RichTextBox to a List<List<string>>, where each
element of the List will be a List<string> containing one line of the RichTextBox parsed into words:
private char[] splitLines = new char[] { '\n' };
private char[] splitWords = new char[] { ' ' };
private List<List<string>> ParseRichTextBox(RichTextBox rtfBox)
{
List<string> rtfLineList = new List<string>(rtfBox.Text.Split(splitLines, StringSplitOptions.RemoveEmptyEntries));
List<List<string>> rtfLineWordList = new List<List<string>>
(
rtfLineList.ConvertAll<List<string>>
(
(string s) =>
{
return new List<string>
(s.Split(splitWords, StringSplitOptions.RemoveEmptyEntries));
}
)
);
return rtfLineWordList;
}
Test:
List<List<string>> parsedRtf = ParseRichTextBox(richTextBox1);
Notes:
1. formatting is deliberately very "spread out" here, to, hopefully, make the structure clearer. you could re-write this in a much terser form.
2. the form of the Linq 'ConvertAll method used here requires C# 3.0 or later.
3. I've been looking for an excuse to use 'ConvertAll :)