Dear Friend

How can I check end of the text in rich text box using C#?

Dani AI

Generated

The thread clarifies that the goal is word-by-word searching inside a RichTextBox (no use of Find()). Earlier suggestions from (copy to a string and search), (split into words) and (regex splitting) are valid starting points but miss a few practical details: punctuation/newlines, whole-word boundaries, preserving the caret/selection, and performance on large text.

A simple test for “caret at end” (useful when scanning) is to compare selection position to the text length:

bool CaretAtEnd(RichTextBox rtb)
{
    return rtb.SelectionStart + rtb.SelectionLength >= rtb.Text.Length;
}

For robust whole-word searching and to get exact character indices (so matches can be highlighted or processed without partial-word hits), use a word-boundary regex and iterate matches. This also avoids manual splitting and keeps punctuation and newlines handled cleanly. Store and restore the original selection when highlighting so the user's caret isn’t lost.

using System.Text.RegularExpressions;

void HighlightWord(RichTextBox rtb, string word)
{
    var origStart = rtb.SelectionStart;
    var origLen = rtb.SelectionLength;

    string pattern = @"\b" + Regex.Escape(word) + @"\b";
    var matches = Regex.Matches(rtb.Text, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);

    foreach (Match m in matches)
    {
        rtb.Select(m.Index, m.Length);
        rtb.SelectionBackColor = Color.Yellow;
    }

    rtb.Select(origStart, origLen); // restore selection
}

Practical notes: prefer rtb.Text (not RTF) for textual searches; use Regex.Escape to handle special characters in the search term; choose RegexOptions.Compiled only if searches run frequently; for culture-sensitive comparisons consider RegexOptions.CultureInvariant or explicit StringComparison when using IndexOf. This approach ties back to the split/IndexOf ideas already posted but gives a reliable, whole-word-aware workflow that preserves formatting and selection.

Recommended Answers

All 12 Replies

i dont understand your question.

Neither me..

no not again :D
ddanbe,sknake, adatapost and others will be added to list soon :)

I have one richtext box in my windows application and wrote few lines in that richtext box say - 7 lines
Now I want to read all the text of richtext box word by word and want to search particular word in those text.
I know there is find() in C# but I don't want to use that.

I hope now I am clear to every body

You can copy the text from the RichTextBox into a String and search it using IndexOf method.

To get word by word then search on specific word

int GetWordIndex(string word)
{
char[] splitters = new char[]{' '};
string[] wordsArr = RichTextBox1.Text.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i< wordsArr.Length; i++)
if(string.Equals(wordsArr[i], word, StringComparison.InvariantCultureIgnoreCase))
return i;
return -1; //indicates there is no match
}
commented: ramy got it again! +4

What do you want to use?

private void button1_Click(object sender, EventArgs e)
    {
      string find = "blu";
      if (0 <= richTextBox1.Text.IndexOf(find, StringComparison.InvariantCultureIgnoreCase))
      {
        System.Diagnostics.Debugger.Break();
        //found
      }
      else
      {
        // not found
      }
    }

To get word by word then search on specific word

int GetWordIndex(string word)
{
char[] splitters = new char[]{' '};
string[] wordsArr = RichTextBox1.Text.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i< wordsArr.Length; i++)
if(string.Equals(wordsArr[i], word, StringComparison.InvariantCultureIgnoreCase))
return i;
return -1; //indicates there is no match
}

You can also use RegEx's word boundary so you can handle splitting and removing punctuation:

private void button2_Click(object sender, EventArgs e)
    {
      string input = richTextBox1.Text;
      string[] words = System.Text.RegularExpressions.Regex.Split(input, @"\W+", System.Text.RegularExpressions.RegexOptions.Multiline);
    }

He can also add in splitters what you need
It may be also array of string to have something like that ("\r\n") if you read from text files.

Actually.. use the split option like Ramy suggested. My original post will match on a partial word :X

Nice piece Ramy , my post was stupid anyway.


You're welcome.
Nothing called stupid, you're trying to help and you should be thanked.
Nice to have you around and trying to help others.

commented: Nice piece of code. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.