Function GetWord(Source: String; WordNumber: Integer): String;
Description: Allows you to choose specified word from any String variable, words are obviously separated by spaces
Original Author(s): Savage
Compilation: Passed
Core Version: 2.8.1 (SC3)
Examples:
Text := '!set money 500';
GetWord(Text, 1); - It will return "!set"
Text := 'multiple spaces are allowed ';
GetWord(Text, 3); - It will return "are"
GetWord(Text, 4); - It will return "allowed"
function GetWord(Source: String; WordNumber: Integer): String;
var
WordCounter, WordIndex, i: Integer;
begin
if WordNumber > 0 then begin
for i := 1 to Length(Source) do
if WordIndex <> 0 then begin
if Source[i] = ' ' then begin
Inc(WordCounter, 1);
if WordNumber = WordCounter then begin
Result := Copy(Source, WordIndex, i-WordIndex);
break;
end else
WordIndex := 0;
end else
if i = Length(Source) then begin
Inc(WordCounter, 1);
if WordNumber = WordCounter then
Result := Copy(Source, WordIndex, i-WordIndex+1);
end;
end else
if Source[i] = ' ' then
continue
else
if i = Length(Source) then begin
Inc(WordCounter, 1);
if WordNumber = WordCounter then
Result := Source[i];
end else
WordIndex := i;
end else
WriteLn('Warning: Function "GetWord" - Second parameter has to be higher than "0"');
end;