split string
чем можно разбить строку на слова?
var s : ansistring;
m : array of ansistring;
procedure  ....
m := s
return количество слов
			
		Модератор: Модераторы

list_of_words = 'слово1 слово2 и т.д.'.split(' ')
assert list_of_words[0] == 'слово1'
assert list_of_words[1] == 'слово2'
assert list_of_words[2] == 'и'
assert list_of_words[3] == 'т.д.'
 .
.

type
  TStringArray = array of string;
function Explode(Ch: Char; const Text: string): TStringArray;
var
  i, k, Len: Integer;
  Count: Integer;
begin
  if Text = '' then
  begin
    Result := nil;
    Exit;
  end; // if
  Count := 0;
  Len := Length(Text);
  for i := 1 to Len do
  begin
    if Text[i] = Ch then Inc(Count);
  end; // for i
  SetLength(Result, Count + 1);
  Count := 0;
  k := 1;
  for i := 1 to Len do
  begin
    if Text[i] = Ch then
    begin
      Result[Count] := Copy(Text, k, i - k);
      Inc(Count);
      k := i + 1;
    end; // if
  end;
  Result[Count] := Copy(Text, k, Len - k + 1);
end;

function WordCount(const S: string; const WordDelims: TSysCharSet): Integer;
function ExtractWord(N: Integer; const S: string;  const WordDelims: TSysCharSet): string;inline;
Вернуться в Free Pascal Compiler
Сейчас этот форум просматривают: Google [Bot] и гости: 1