Author Topic: Function GetWord  (Read 2644 times)

0 Members and 1 Guest are viewing this topic.

Offline Savage

  • Soldier
  • **
  • Posts: 155
Function GetWord
« on: November 18, 2017, 08:23:49 pm »
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"
Code: [Select]
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;
« Last Edit: November 19, 2017, 12:51:05 pm by Savage »

Offline Savage

  • Soldier
  • **
  • Posts: 155
Re: Function GetWord
« Reply #1 on: November 19, 2017, 12:52:48 pm »
Just fixed something, please replace your code