How to get n chars (a string) after a string found....

General TRichView support forum. Please post your questions here
Post Reply
Tavo
Posts: 175
Joined: Sat Sep 24, 2005 9:25 pm
Location: Buenos Aires, Argentina
Contact:

How to get n chars (a string) after a string found....

Post by Tavo »

I have to search within a TRichView a text preceded by a specific string, in example:

'Word1 Word2 blabla blabla bla bla
more bla bla and more bla bla bla
beginhere0123456789finish'

I Want to extract '0123456789' (10 chars after 'beginhere').

How can I do it? Should I seek with SearchText() first?

Thanks.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

SearchText selects the found text.
Call RichViewEdit.TopLevelEditor.GetSelectionBounds to get the position of the selected text. After that you can get text around this position (using GetItemText, GetItemStyle, ItemCount and may be IsFromNewLine methods of RichViewEdit.TopLevelEditor)
Tavo
Posts: 175
Joined: Sat Sep 24, 2005 9:25 pm
Location: Buenos Aires, Argentina
Contact:

Post by Tavo »

Sergey, thanks for the answer. With your help I could do the following. I made my own GetNCharsFromSearch() function:

Given a string (in a TRichViewEdit): "Borland has committed suicide. And only remains in our memory."

I call..

GetNCharsFromSearch( RVE, 'land', 27)

I Get = ' has committed suicide. And' (27 chars)

I do not know if this is the best way. This is the code for those who want to take a look.

Code: Select all

Function GetNCharsFromSearch(
                              rv     : TRichViewEdit ;
                              Cadena : String        ;
                              NChars : Integer
                             ) : String ;

var  No1, No2, Offs1, Offs2: Integer;
     Tmp : String ;
     NItem : Integer ;
begin

  If rv.SearchText( Cadena, [rvseoDown] ) then begin

     rv.TopLevelEditor.GetSelectionBounds(No1,Offs1,No2,Offs2,True);
     Tmp := rv.TopLevelEditor.GetItemText(No1) ;
     Result := Copy(Tmp ,Offs2, (Length(Tmp)-Offs2)+1 ) ;

     NItem := No2 ;

     While (Length(Result)<NChars) and (NItem<rv.ItemCount) do begin
        Inc(NItem) ;
        Result := Result + rv.TopLevelEditor.GetItemText(NItem) ;
     end ;
     Result := Copy(Result,1, NChars) ;
     
  end ;

end ;
 

...
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Change

Code: Select all

    While (Length(Result)<NChars) and (NItem<rv.ItemCount) do begin 
        Inc(NItem) ; 
        Result := Result + rv.TopLevelEditor.GetItemText(NItem) ; 
     end 
to

Code: Select all

    Inc(NItem) ; 
    While (Length(Result)<NChars) and (NItem<rv.ItemCount) do begin 
        if rv.TopLevelEditor.GetItemStyle(NItem)>=0 then
          Result := Result + rv.TopLevelEditor.GetItemText(NItem) 
        else if rv.TopLevelEditor.GetItemStyle(NItem)=rvsTab then
          Result := Result + #9; 
        Inc(NItem) ; 
     end 
Also, do you want to accumulate text even if it belons to several paragraphs? If not, add

Code: Select all

  if rv.TopLevelEditor.IsFromNewLine(NItem) then
    break;
at the beginning of this cycle.
Post Reply