Current Text

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
Hi Bill

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;
}


Happy coding!
Morten Wennevik [C# MVP]
 
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;
}

A somewhat more efficient alternative:

string GetWord()
{
if (rtb.TextLength==0)
{
return "";
}

string text = rtb.Text;

int end = rtb.SelectionStart;
int start = text.LastIndexOf (' ', end);
if (start==-1)
{
start = 0;
}
return text.Substring (start, end-start);
}
 
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.

Happy coding!
Morten Wennevik [C# MVP]
 
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.

No, note that I used the version of LastIndexOf which takes two
parameters - the second parameter is the index at which to start the
search. So, for instance, "hello there Morton".LastIndexOf(' ', 8)
returns 5.

The OP may wish to tailor things considering what he wishes to happen
if the current selection *is* a space, however.
 
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.

Happy coding!
Morten Wennevik [C# MVP]
 
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.

Yes, the general description isn't great. The more detailed description
makes more sense.
 
Change that to

text.LastIndexOf(' ', 8) == ((text.SubString(8)).LastIndexOf(' ') + 8)

The first returns 5, and the other returns 12 (4+8).

Happy coding!
Morten Wennevik [C# MVP]
 
Back
Top