Dear Friend

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

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.

@Lighthead
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.