procedure ExtractWord(s:string;Index:integer;var Position,Len:Integer);
var
i,n,count,pos1,pos2:Integer;
Start:Boolean;
begin
n:=Length(s);
Start:=false;
count:=1;
Pos1:=0;
Pos2:=0;
for i:=1 to n do
begin
if s in ['a'..'z','A'..'Z'] then
begin
Start:=true;
if (count=Index) and (Pos1=0) then
Pos1:=i;
end
else if Start then
begin
Start:=false;
Inc(count);
if count>Index then
begin
Pos2:=i;
break;
end;
end;
end;
if (Pos1>0) and (Pos2=0) then
Pos2:=n+1;
Len:=Pos2-Pos1;
Position:=Pos1;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
loc,len:Integer;
str:String;
begin
str:='Hello! My friend.Nice to meet you';
ExtractWord(str,4,loc,len);
Caption:=Format('Loc:%d Len=%d',[loc,len]);
end;