我很困惑,在Delphi 7的备忘录中得到一个特定的单词开头行
我有类似的东西
Memo1.lines.text:= *Some execution process*;
,Memo1中的结果为
I have something to do
I have never gone there
We are playing Tenis
You are so sweet
I am going there
现在我怎么才能得到以我们而不是整行开头的行
就像上面的备忘录
一样我们正在玩Tenis
答案 0 :(得分:3)
如果您知道您总是想要一个特定的行(如样本中的第三行),请直接访问它。与Lines
一样,TStringList
从零开始,因此减去一个:
YourLine := Memo1.Lines[2];
如果您只有部分文字位于该行的开头,则可以遍历每一行并使用StrUtils.StartsText
:
var
i: Integer;
FoundAt: Integer;
begin
FoundAt := -1;
for i := 0 to Memo1.Lines.Count - 1 do
begin
if StrUtils.StartsText('We', Memo1.Lines[i]) then
FoundAt := i;
end;
if FoundAt <> -1 then
YourText := Memo1.Lines[FoundAt];
end;
在Delphi 7中,改为使用AnsiStartsText
(也可在StrUtils
单元中找到)。
答案 1 :(得分:1)
使用StrUtils
Function sExtractBetweenTagsB(Const s, LastTag, FirstTag: string): string;
var
pLast,pFirst,pNextFirst : Integer;
begin
pFirst := Pos(FirstTag,s);
pLast := Pos(LastTag,s);
while (pLast > 0) and (pFirst > 0) do begin
if (pFirst > pLast) then // Find next LastTag
pLast := PosEx(LastTag,s,pLast+Length(LastTag))
else
begin
pNextFirst := PosEx(FirstTag,s,pFirst+Length(FirstTag));
if (pNextFirst = 0) or (pNextFirst > pLast) then begin
Result := Trim(Copy(s,pFirst,pLast-pFirst+Length(LastTag)));
Exit;
end
else
pFirst := pNextFirst;
end;
end;
Result := '';
end;
像这样的用法
procedure TForm24.btn1Click(Sender: TObject);
begin
ShowMessage(sExtractBetweenTagsB(Memo1.Text,sLineBreak,'We'));
//or
ShowMessage(sExtractBetweenTagsB(Memo1.Text,'Tenis','We'));
end;
输出:We are playing Tenis
答案 2 :(得分:0)
j := FirstLineStarting( Memo1.Lines, 'we' );
if j >= 0 then
ShowMessage( Memo1.Lines[j] );
.....
function FirstLineStarting( const Text: TStrings; const Pattern: string; const WithCase: Boolean = false): integer;
var i: integer;
check: function (const ASubText, AText: string): Boolean;
begin
Result := -1; // not found
if WithCase
then check := StartsStr
else check := StartsText;
for i := 0 to Text.Count - 1 do
if check( Pattern, Text[i] ) then begin
Result := i;
break;
end;
end;