procedure SplitTextIntoWords(const S: string; words: TstringList);
var
startpos, endpos: Integer;
begin
Assert(Assigned(words));
words.Clear;
startpos := 1;
while startpos <= Length(S) dobegin// skip non-letters while (startpos <= Length(S)) andnot IsCharAlpha(S[startpos]) do
Inc(startpos);
if startpos <= Length(S) thenbegin// 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;
beginifnot case_sensitive thenbegin
S := AnsiUpperCase(S);
mask := AnsiUpperCase(mask);
end; { If }
Result := True; // blatant optimism
sIndex := 1;
maskIndex := 1;
while (sIndex <= Length(S)) and (maskIndex <= Length(mask)) dobegincase 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
elseif mask[maskindex] in ["*", "?"] thenraise 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) thenbegin// character not found, no match
Result := False;
Exit;
end;
{ If }end; { Case "*" }elseif S[sIndex] = mask[maskIndex] thenbegin
Inc(sIndex);
Inc(maskIndex);
end{ If }elsebegin// 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 dobeginif 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;
Если Вас заинтересовала или понравилась информация по разработке на Delph - "Разделить строку на слова", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу: Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!