Delphi-Help

  • Increase font size
  • Default font size
  • Decrease font size
Главная Статьи Текст и Строки Заполнение списка словами из строки

Заполнение списка словами из строки

Оцените материал
(0 голосов)

Заполнение списка словами из строки

Функция заполняет список TargetList, словами (наборами символов) из строки Text. Имеется возможность получения позиции каждого слова в строке (ReturnWordPlaces = True); добавления в TargetList не только слов, но и разделителей (ReturnWordDeviders = True); указания более чем одного разделителя (все в строке WordDeviders). Ограничением является невозможность указания разделителя, длинной более чем 1 символ.

Result = TargetList.Count; (количество строк в TargetList)

function GetWordListFromText(Text, WordDeviders: string; TargetList: TStrings;
  ReturnWordPlace, ReturnWordDeviders: Boolean): Integer;
var
  X, TextLength, WP: Integer;
  W: string;
begin
  Result := 0;
  TextLength := Length(Text);
  if TextLength = 0 then
    Exit;
  if Length(WordDeviders) = 0 then
    Exit;
  if TargetList = nil then
    Exit;
  TargetList.BeginUpdate();
  TargetList.Clear;
  WordDeviders := AnsiUpperCase(WordDeviders);
  W := '';
  X := 0;
  WP := 1;
  repeat
    X := X + 1;
    if (POS(AnsiUpperCase(Text[x]), WordDeviders) = 0) and (X <= TextLength)
      then
      W := W + Text[x]
    else
    begin
      if W <> '' then
      begin
        case ReturnWordPlace of
          True: TargetList.Add(W + '=' + Inttostr(WP));
          False: TargetList.Add(W);
        end;
      end;
      W := '';
      WP := X + 1;
      if ReturnWordDeviders = true then
      begin
        case ReturnWordPlace of
          True: TargetList.Add(Text[x] + '=' + Inttostr(x));
          False: TargetList.Add(TEXT[x]);
        end;
      end;
    end;
  until (X > TextLength);
  TargetList.EndUpdate;
  Result := TargetList.Count;
end;
Прочитано 7102 раз

Авторизация



Счетчики