Delphi 找出最长的单词
Delphi 找出最长的一个单词、用字符串
补充:Delphi 的完整代码
Delphi 找出最长的一个单词、用字符串
补充:Delphi 的完整代码
答案:
procedure TForm1.Button1Click(Sender: TObject);
function rtStrList(AStrList: TStringList): string;
var
idx: Integer;
iTag: Integer;
begin
iTag := 0;
for idx := 0 to Pred(AStrList.Count) do
begin
if Length(AStrList[idx]) > iTag then
begin
iTag := Length(AStrList[idx]);
Result := AStrList[idx];
end
else if Length(AStrList[idx]) = iTag then
begin
Result := Result + '+' + AStrList[idx]; //可能有长度一样的
end;
end;
end;
var
Letter: Char;
Str, rStr: string;
iStr, iTag: Integer;
iWord, iPcttn, iSpace: Integer;
WordList: TStringList;
begin
iStr := 0;
iTag := 0; // 标记单词字符数
iWord := 0; // 单词数
iPcttn := 0; // 符号数
iSpace := 0; // 空格数
Str := Memo1.Text + ' '; // 处理最后一个单词结尾
if Length(Str) <> 1 then
begin
WordList := TStringList.Create;
WordList.Sorted := True; // 需要先指定排序
WordList.Duplicates := dupIgnore; // 如有重复值则放弃
try
for iStr := 1 to Length(Str) do
begin
Letter := Str[iStr];
if (Letter in ['a' .. 'z']) or (Letter in ['A' .. 'Z']) then // 单词字符
begin
inc(iTag);
end
else if Letter in [' ', ',', '.', '''', '?', '!'] then // 符号、空格
begin
if Letter = ' ' then
inc(iSpace)
else
inc(iPcttn);
if iTag > 0 then
begin
inc(iWord);
WordList.Add(Copy(Str, iStr - iTag, iTag));
iTag := 0;
end;
end;
end;
{ printf }
Label1.Caption := '单词:' + IntToStr(iWord);
Label2.Caption := '标点:' + IntToStr(iPcttn);
Label3.Caption := '空格:' + IntToStr(iSpace - 1);
{ }
if WordList.Count > 0 then
begin
ShowMessage(rtStrList(WordList));
end;
finally
WordList.Free;
end;
end;
end;循环+length(Trim(value))
上一个:这段delphi 代码怎么简化?
下一个:会vb是不是差不多会delphi