G
Guest
I have a richtextbox, anybody have any ideas for me to be able to get the string from the last spacebar to the current position?
Morten Wennevik said:Will this give you what you want?
private string GetWord()
{
if(rtb.TextLength == 0)
return "";
string word = "";
for(int i = rtb.SelectionStart; i > 0; i--)
{
char c = rtb.Text[i-1];
if(c == ' ')
break;
word = c + word;
}
return word;
}
Morten Wennevik said:Yes, I thought of that, but lastindexof reports the last ' ' of the
text where the search begins at selectionstart. This is all fine if
selectionstart actually is at the end of the text, but otherwise you
might find spaces after current position and run the risk of
substringing a negative number.
Morten Wennevik said:Ah, sorry, the phrasing in the help file confused me.
IndexOf(char value, int startIndex)
LastIndexOf(char value, int startIndex)
They should have changed it to endIndex. Also the helpfile describes startIndex as
"The starting position of a substring within this instance." which I read to be
string text = "Hello Morten Wennevik";
text.LastIndexOf(' ', 8) == (text.SubString(8)).IndexOf(' ');
It may be that my English skills are lacking, but I find the helpfile
very confusing in this case.