我有一个处理程序,在其中比较从用户接收到的单词的长度,并将其与用户在InputCountFromUser中输入的数字进行比较,如果它们匹配,我会在OutputWordsFromStr中输入该单词。
但是如何输入一行并将其中的每个单词与InputCountFromUser进行比较,并在OutputWordsFromStr中显示与用户输入的数字匹配的单词,即InputCountFromUser
示例: ``InputStrFromUser:='嗨汤姆!你好吗?' 来自用户的输入计数:= 3;并在 Memo (OutputWordsFromStr) 中显示单词 Tom 和 How。
procedure TForm1.OutputBtnClick(Sender: TObject);
var
InputStr: string;
gg:integer;
begin
InputStr:= InputStrFromUser.Text;
OutputWordsFromStr.Clear;
if ( IntToStr(length(InputStr)) = InputCountFromUser.Text) then
begin
OutputWordsFromStr.Text:= InputStr;
end;
end;
解决方案:
procedure TForm1.OutputBtnClick(Sender: TObject);
var
inputStr: string;
words: TStringList;
i:integer;
begin
inputStr := InputStrFromUser.Text;
OutputWords.Clear;
words:= TStringList.Create;
words.DelimitedText:= inputStr;
ShowMessage(IntToStr(words.Count));
for i := 0 to words.Count - 1 do
begin
if IntToStr(Length(words[i])) = InputCountFromUser.Text then
begin
OutputWords.Lines.Add(words[i]);
end;
end;
end;
唯一分隔单词的是空格,即行中有Привет мир!
2 个单词Привет
和мир!
。
该标志!
也被考虑在内。
您可以将字符串中的单词提取到 TStringList 或字符串数组中。德尔福有Split。
如果 FreePascal 突然没有这样的功能,请深入研究 StrUtils - 我看到SplitString