Delphi中有双引号字符串函数吗?

In Delphi is there a double quote string function?

我知道 QuotedStr 函数,但是是否有类似的双引号函数

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

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

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

来自documentation

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

....

Use AnsiQuotedStr to convert a string (S) to a quoted string, using the provided Quote character. A Quote character is inserted at the beginning and end of S, and each Quote character in the string is doubled.

在较新的 Delphi 版本中,如果包含 System.SysUtils,则可以使用带有参数 '"':

的字符串辅助函数 TStringHelper.QuotedString
'Test'.QuotedString('"')

这将 return "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;