Page 1 of 1
How to get n chars (a string) after a string found....
Posted: Wed Nov 19, 2008 5:41 pm
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.
Posted: Thu Nov 20, 2008 10:31 am
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)
Posted: Thu Nov 20, 2008 5:57 pm
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 ;
...
Posted: Thu Nov 20, 2008 6:51 pm
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.