Dear Friend

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

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

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
}
Comments
ramy got it again!

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

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

Comments
Nice piece of code.
This article has been dead for over six months. Start a new discussion instead.