function WortAnPos(s: String; p: Integer): String;
var
i: Integer;
begin
if Pos(' ', s) < p then
begin
for i := p downto 1 do
if s[i] = ' ' then
begin
Delete(s, 1, i);
Break;
end;
end;
Result := s;
if Pos(' ', Result) > 0 then
Delete(Result, Pos(' ', Result), maxint);
end;
function _darunter(list: TStrings; Index, p: Integer): String;
var
s: String;
begin
if Index = 0 then Exit;
Result := WortAnPos(list[Index - 1], p);
end;
function _darueber(list: TStrings; Index, p: Integer): String;
begin
if Index = list.Count - 1 then Exit;
Result := WortAnPos(list[Index + 1], p);
end;
function _links(s: String; p: Integer): String;
begin
Delete(s, p, Maxint);
if LastDelimiter(' ', s) > 0 then
Delete(s, LastDelimiter(' ', s), Maxint);
if LastDelimiter(' ', s) > 0 then
Result := Trim(Copy(s, LastDelimiter(' ', s), Maxint))
else
Result := Trim(s);
end;
function _rechts(s, substr: String; p: Integer): String;
begin
Delete(s, 1, p + length(substr));
if Pos(' ', s) > 0 then
begin
Delete(s, 1, Pos(' ', s));
Result := Trim(Copy(s, 1, Pos(' ', s)))
end
else
Result := Trim(s);
end;
procedure TForm1.Button1Click(Sender: TObject);
const
gesucht = 'bb';
var
i, p: Integer;
s: String;
begin
Memo1.Clear;
Memo1.Lines.Add('1111 2222 3333 4444');
Memo1.Lines.Add('aaaa bbbb cccc dddd');
Memo1.Lines.Add('5555 6666 7777 8888');
for i := 0 to Memo1.Lines.Count - 1 do
begin
s := Memo1.Lines[i];
p := Pos(gesucht, s);
if p > 0 then
begin
ShowMessage('Links: ' + _links(s, p));
ShowMessage('Rechts: ' + _rechts(s, gesucht, p));
ShowMessage('Draunter: ' + _darunter(Memo1.Lines, i, p));
ShowMessage('Draunter: ' + _darueber(Memo1.Lines, i, p));
end;
end;
end;