简体   繁体   中英

In Delphi is there a double quote string function?

I'm aware of the QuotedStr function, but is there a similar function for double quoting for example

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

You can use AnsiQuotedStr which accepts a quote character:

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

From the 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.

In the newer Delphi versions, if you include System.SysUtils , you can use the string helper function TStringHelper.QuotedString with parameter '"' :

'Test'.QuotedString('"')

This will return "Test" .

I made a small unit test for it:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM