1

Im looking for a function that will enable me to find a certain string in a textRange and gives me back its textPointers(beginning and end) whats the best approach or is there any textRange method that can help me ? Thanks.

I have a problem in my approach which happens when i have same words in my textRange and im trying to get the Textpointer for each word in the textRange.
For example my text in the textRange is
"hello hello hello"
and i want to get the text pointer for each word in the textRange, it will mix the second and last word and give me (the first indexOf the word) or last depends on which method i use.

H H
  • 263,252
  • 30
  • 330
  • 514
raym0nd
  • 3,172
  • 7
  • 36
  • 73
  • Are textRange and textPointer classes in some API? If so, which API? – H H Jul 04 '11 at 19:26
  • can I see an example of what you mean? – Spooks Jul 04 '11 at 19:26
  • I think it's referring to this http://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.aspx – John K Jul 04 '11 at 19:28
  • @Slaks @HenkHolterman @Spooks lets say i have a textRange tr; i want to search for a string text = "apple"; inside the text range and i want to get if its in the textRange, if so i want to find its beginning textPointer and end textPointer – raym0nd Jul 04 '11 at 19:28
  • 1
    @raymOnd, can you update the question with .NET namespace or API name, since textrange exists in different APIs. Just to make life easier for people visiting this question in the future. – John K Jul 04 '11 at 19:31
  • @henk TextRange represents a selection of content between two TextPointer positions. check this [http://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.aspx] – raym0nd Jul 06 '11 at 17:12
  • @Henk thanks I'm still new in here. – raym0nd Jul 06 '11 at 19:57

1 Answers1

0

Something like this

string txt = tr.Text; //tr is your TextRange
string find = "a str";
int beginPtr = txt.IndexOf(find);
int endPrt = beginPtr + find.Length;
ata
  • 8,853
  • 8
  • 42
  • 68
  • this is fine for a textRange that contains a simple text, but if ur textRange has alot of format, color and different font size, this wont work properly. this is what I did – raym0nd Jul 04 '11 at 21:04
  • Why is all the formatting, font size part of the text? Anyway, I think you need some sort of regular expression. I am not expert in regular expressions so cannot write you one. But you still have to provide list of all the formatting characters etc for someone to write the regular expression. – ata Jul 04 '11 at 21:27
  • because im getting my textRange from a RichtextBox that gives the text with formats and line breaks, runs, and so on.. I'm sorry i wasnt clear in the beginning. but ur answer works fine for normal text :) – raym0nd Jul 05 '11 at 13:16