Alister
Alister

Reputation: 6837

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;

Upvotes: 3

Views: 9523

Answers (2)

davepaul12
davepaul12

Reputation: 391

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;

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 613322

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.

Upvotes: 12

Related Questions