Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  136

•  TDictionary Custom Sort  3 318

•  Fast Watermark Sources  3 065

•  3D Designer  4 825

•  Sik Screen Capture  3 321

•  Patch Maker  3 536

•  Айболит (remote control)  3 637

•  ListBox Drag & Drop  2 996

•  Доска для игры Реверси  81 568

•  Графические эффекты  3 927

•  Рисование по маске  3 232

•  Перетаскивание изображений  2 613

•  Canvas Drawing  2 735

•  Рисование Луны  2 561

•  Поворот изображения  2 166

•  Рисование стержней  2 161

•  Paint on Shape  1 564

•  Генератор кроссвордов  2 226

•  Головоломка Paletto  1 764

•  Теорема Монжа об окружностях  2 217

•  Пазл Numbrix  1 682

•  Заборы и коммивояжеры  2 052

•  Игра HIP  1 279

•  Игра Go (Го)  1 225

•  Симулятор лифта  1 471

•  Программа укладки плитки  1 214

•  Генератор лабиринта  1 542

•  Проверка числового ввода  1 352

•  HEX View  1 490

•  Физический маятник  1 355

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Разделить строку на слова



Оформил: DeeCo

procedure SplitTextIntoWords(const S: string; words: TstringList);
 var
   startpos, endpos: Integer;
 begin
   Assert(Assigned(words));
   words.Clear;
   startpos := 1;
   while startpos <= Length(S) do
   begin
     // skip non-letters 
    while (startpos <= Length(S)) and not IsCharAlpha(S[startpos]) do
       Inc(startpos);
     if startpos <= Length(S) then
     begin
       // find next non-letter 
      endpos := startpos + 1;
       while (endpos <= Length(S)) and IsCharAlpha(S[endpos]) do
         Inc(endpos);
       words.Add(Copy(S, startpos, endpos - startpos));
       startpos := endpos + 1;
     end; { If }
   end; { While }
 end; { SplitTextIntoWords }

 function StringMatchesMask(S, mask: string;
   case_sensitive: Boolean): Boolean;
 var
   sIndex, maskIndex: Integer;
 begin
   if not case_sensitive then
   begin
     S    := AnsiUpperCase(S);
     mask := AnsiUpperCase(mask);
   end; { If }
   Result    := True; // blatant optimism 
  sIndex    := 1;
   maskIndex := 1;
   while (sIndex <= Length(S)) and (maskIndex <= Length(mask)) do
   begin
     case mask[maskIndex] of
       '?':
         begin
           // matches any character 
          Inc(sIndex);
           Inc(maskIndex);
         end; { case '?' }
       '*':
         begin
           // matches 0 or more characters, so need to check for 
          // next character in mask 
          Inc(maskIndex);
           if maskIndex > Length(mask) then
             // * at end matches rest of string 
            Exit
           else if mask[maskindex] in ['*', '?'] then
             raise Exception.Create('Invalid mask');
           // look for mask character in S 
          while (sIndex <= Length(S)) and
             (S[sIndex] <> mask[maskIndex]) do
             Inc(sIndex);
           if sIndex > Length(S) then
           begin
             // character not found, no match 
            Result := False;
             Exit;
           end;
           { If }
         end; { Case '*' }
       else if S[sIndex] = mask[maskIndex] then
         begin
           Inc(sIndex);
           Inc(maskIndex);
         end { If }
         else
           begin
             // no match 
            Result := False;
             Exit;
           end;
     end; { Case }
   end; { While }
   // if we have reached the end of both S and mask we have a complete 
  // match, otherwise we only have a partial match 
  if (sIndex <= Length(S)) or (maskIndex <= Length(mask)) then
     Result := False;
 end; { stringMatchesMask }

 procedure FindMatchingWords(const S, mask: string;
   case_sensitive: Boolean; matches: Tstrings);
 var
   words: TstringList;
   i: Integer;
 begin
   Assert(Assigned(matches));
   words := TstringList.Create;
   try
     SplitTextIntoWords(S, words);
     matches.Clear;
     for i := 0 to words.Count - 1 do
     begin
       if stringMatchesMask(words[i], mask, case_sensitive) then
         matches.Add(words[i]);
     end; { For }
   finally
     words.Free;
   end;
 end;

 { 
 The Form has one TMemo for the text to check, one TEdit for the mask, 
 one TCheckbox (check = case sensitive), one TListbox for the results, 
 one Tbutton 
}
 procedure TForm1.Button1Click(Sender: TObject);
 begin
   FindMatchingWords(memo1.Text, edit1.Text, checkbox1.Checked, listbox1.Items);
 end;







Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте