Delphi中返回由空格分隔的字符串中单词的总数

应用场景

有如下一个字符串:
‘苹果 香蕉 火龙果’ 是3个词语,返回3
如果"苹果 香蕉"用双引号就表示是一个整体算1个词语,返回2

函数代码

function CountUnquotedWords(const S: string; const AQuote: Char = '"'): Integer;
begin
  with TStringList.Create do
  try
    QuoteChar := AQuote;
    Delimiter := ' ';
    DelimitedText := S;
    Result := Count;
  finally
    Free;
  end;
end;

测试

  caption:= CountUnquotedWords('苹果 香蕉 火龙果').ToString;
  //caption:= CountUnquotedWords('"苹果 香蕉" 火龙果').ToString;
···

你可能感兴趣的:(delphi常用函数,字符串)