Форум ООП

Функция выделения слов из строки

Функция выделения слов из строки

by Deleted user -
Number of replies: 0

Из http://kladovka.net.ru/delphibase/?action=viewfunc&topic=strlists&id=10545

function GetWords(const S: string; L: TStrings; Delimiters: TSysCharSet): integer;
var len, idx1, idx2: integer;
begin
     Result := 0;
     if Length(S) = 0 then Exit;
     L.Clear;
     len := Length(S);
     idx2 := 1;
     repeat
       while (idx2 <= len) and (S[idx2] in Delimiters) do inc(idx2);
       idx1 := idx2;
       if (idx2 <= len) and not (S[idx2] in Delimiters) then
         while (idx2 <= len) and not(S[idx2] in Delimiters) do inc(idx2);
       if idx1 < idx2 then
          L.Add(Copy(S, idx1, idx2-idx1));
     until idx2 > len;
     Result := L.Count;
end;

procedure TForm1.Button1Click(Sender: TObject);
var S: string;
begin
  S := 'Строка1,Строка2;Строка3;;: Строка4,,,,,';
  GetWords(S, ListBox1.Items, [',', ';', ':', ' ']);

  { Результат:
        Строка1
        Строка2
        Строка3
        Строка4
  }
end;