Консультация № 174578
27.11.2009, 05:23
35.00 руб.
0 3 1
Подскажите с помощью этой функции вырезаю содержимое между первым тегом \row и закрывающим \ row } проблема в том что у меня каждая строчка имеет свой тег \row как сделать чтобы он первый тег пропускал и вырезал начиная с 2-3 тега



Приложение:
function GetInnerText(const Source, StartFragment, EndFragment: string): string;
var
i:integer;
PosStart, PosEnd: Integer;
begin
PosStart := Pos(StartFragment, Source);
if PosStart > 0 then
begin
Inc(PosStart, Length(StartFragment));
if PosStart < Length(Source) then
begin
PosEnd := Pos(EndFragment, Source) - Length(EndFragment);
if PosEnd > PosStart then
begin
Result := Copy(Source, PosStart, PosEnd - PosStart + 10);
Exit;
end;
end;
end;
end;

Обсуждение

Неизвестный
27.11.2009, 14:27
общий
это ответ
Здравствуйте, Kensh1n.

Предлагаю Вам несколько переработанный вариант :

uses StrUtils;

// StartParseFrom - номер символа в строке, с которого будет производиться обработка
function GetInnerText(const Source, StartFragment, EndFragment: string; StartParseFrom:integer): string;
var
i:integer;
PosStart, PosEnd: Integer;
begin
PosStart := PosEx(StartFragment, Source,StartParseFrom);
if PosStart > 0 then
begin
Inc(PosStart, Length(StartFragment));
if PosStart < Length(Source) then
begin
PosEnd := PosEx(EndFragment, Source, PosStart+Length(StartFragment));
if PosEnd > PosStart then
begin
Result := Copy(Source, PosStart, PosEnd - PosStart);
Exit;
end;
end;
end;
end;

// Пример использования
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:='\row что-то бесполезное \row наша строка, котрую нам нужно вырезать \ row';
Edit1.Text:=
GetInnerText(s,'\row','\ row',Pos('\row',s)+1);
// в Edit1 появится следующий тект - " наша строка, котрую нам нужно вырезать "
end;

Обращаю Ваше внимание на то что необходимо добавить модуль StrUtils в uses.
5
Неизвестный
27.11.2009, 15:30
общий
Paul80:
Большое спасибо за оперативность есть только 1н маленький вопрос проблема в том что я пишу прогу на delphi 5 а насколько я знаю функцию posex в strutils добавили только в delphi 7, не будет проблемы если я добавлю функцию в свою программульку и объявлю как пользовательскую?
Неизвестный
27.11.2009, 15:47
общий
Kensh1n:
добавьте в код до функции GetInnerText вот так:

{ PosEx searches for SubStr in S and returns the index position of
SubStr if found and 0 otherwise. If Offset is not given then the result is
the same as calling Pos. If Offset is specified and > 1 then the search
starts at position Offset within S. If Offset is larger than Length(S)
then PosEx returns 0. By default, Offset equals 1. }

function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;
var
I,X: Integer;
Len, LenSubStr: Integer;
begin
if Offset = 1 then
Result := Pos(SubStr, S)
else
begin
I := Offset;
LenSubStr := Length(SubStr);
Len := Length(S) - LenSubStr + 1;
while I <= Len do
begin
if S[I] = SubStr[1] then
begin
X := 1;
while (X < LenSubStr) and (S[I + X] = SubStr[X + 1]) do
Inc(X);
if (X = LenSubStr) then
begin
Result := I;
exit;
end;
end;
Inc(I);
end;
Result := 0;
end;
end;
Форма ответа