Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam splitting words in richtextbox with space as delimeter
and each word has rtf
am saving word text and word rtf in database and retrieving and showing in datagridview

But problem is some word rtfs changed from actual rtf
so am not getting actual words

My code is

string[] parsList = rtxtPAR.Text.Split(new string[] {"\n" }, StringSplitOptions.RemoveEmptyEntries);
LstNewPAR = new List<string>(parsList);

XML
private List<string> GenerateRtfForList()
        {

            LstNewPAR_Rtf = new List<string>();
            string[] splitter = { "\n" };
            char[] delim = { '\n' };
            //string[] LstNewPAR = rtxtPAR.Text.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
            var lstNewParText = rtxtPAR.Text.Split(delim, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < LstNewPAR.Count(); i++)
            {

                rtxtPAR.Text = lstNewParText[i];

                HilightRichTextBoxText();
                LstNewPAR_Rtf.Add(rtxtPAR.Rtf);
            }



            return LstNewPAR_Rtf;

        }



any suggestions
Posted
Comments
BillWoodruff 29-Jan-14 2:07am    
Your code does not show you splitting the RTF-Text by words: it only shows splitting by lines.

"word rtfs changed from actual rtf" ... please give specific examples

What is the final result you want ?

One big Array or List<string> of all the words ?

An Array of Arrays ? A List<List<string>> ?

1 solution

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:
C#
// define in Form scope
private char[] splitLines = new char[] { '\n' };
private char[] splitWords = new char[] { ' ' };

// RichTextBox => List<List<string>>;
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:
// assume a RichTextBox named 'richTextBox1 on a Form
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 :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900