在Delphi中是否有双引号字符串函数?

时间:2016-04-06 21:37:13

标签: string delphi double-quotes

我知道QuotedStr函数,但是有一个类似的双引号函数,例如

for i := 0 to List.count - 1 do
begin
  List[i] := DoubleQuotedStr(List[i]);
end;

2 个答案:

答案 0 :(得分:9)

您可以使用接受引号字符的AnsiQuotedStr

List[i] := AnsiQuotedStr(List[i], '"');

来自documentation

function AnsiQuotedStr(const S: string; Quote: Char): string;
     

...

     

使用AnsiQuotedStr将字符串(S)转换为带引号的字符串,使用提供的Quote字符。在S的开头和结尾插入引号字符,字符串中的每个引号字符加倍。

答案 1 :(得分:2)

在较新的Delphi版本中,如果包含System.SysUtils,则可以将字符串帮助器函数TStringHelper.QuotedString与参数'"'一起使用:

'Test'.QuotedString('"')

这将返回"Test"

我为此做了一个小单元测试:

uses 
  System.SysUtils, DUnitX.TestFramework;

(...)

procedure TStringFunctionsTests.TestWithQuotedString;
var
  TestString: string;
  ExpectedResult: string;
  TestResult: string;
begin
  TestString := 'ABC';
  ExpectedResult := '"ABC"';
  TestResult := TestString.QuotedString('"');
  Assert.AreEqual(TestResult, ExpectedResult);
end;