How to extract last word in richtextbox in c #

I am working with a windows form. In my project, I need to color the last word of a rich text field. When someone writes in this text box of the application, I need the last word that has just been written in richtextbox to be colored red or something else.

I found a way to extract the last word from the following link

http://msdn.microsoft.com/en-us/library/system.windows.documents.textselection.select%28v=vs.95%29.aspx

But I need a more convenient code to extract the last word, if possible. Please help.

-3
c # extract richtextbox word
source share
3 answers

Here is a sample code

*> char [] arr = new char [50];

int i = 0; private void richTextBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Space) { string str = new string(arr); MessageBox.Show(str); Array.Clear(arr, 0, arr.Length); i = 0; } else if (e.KeyCode == Keys.Back) { i--; if (i < 0) { i = 0; } arr[i] = ' '; } else { arr[i] = (char)e.KeyValue; i++; } }* 

Here's how you can extract the last word. Now leave yourself a word that you like.

+1
source share

Well, if you really want to get the last word, you can do something like this ... Assuming, of course, that you are creating a line equal to the text of your rich text box.

 string str="hello, how are you doing?"; if (str.Length >0) { int index=str.LastIndexOf(" ") + 1; str = str.Substring(index)); } 

Then just return the line and do what you need to do with it.

+2
source share

As I recall, a rich text box can display text as HTML.
Just wrap your last word in the font tag and it.

-one
source share

All Articles