Search code examples
delphiescapingword-wrapdelphi-2007

Using SysUtils.WrapText() with strings containing single quotes


I'm trying to use the SysUtils.WrapText() function with a string containing escaped single quotes characters, and I'm getting an unexpected result.

var
  Lines : TStrings;
begin
  Lines := TStringList.Create;
  try
    Lines.Text := WrapText('Can''t format message, message file not found', 15);
    ShowMessage(Lines.Text);
  finally
    Lines.Free;
  end;
end;

It seems that the function doesn't wrap the string at all, if the string contains an apostrophe character.

I've also tried using the #39 code instead of the single quote char but the problem persists. Furthermore I've checked Lines.Count and it's 1.

image

I've tried removing the single quote character:

var
  Lines : TStrings;
begin
  Lines := TStringList.Create;
  try
    Lines.Text := WrapText('Cant format message, message file not found', 15);
    ShowMessage(Lines.Text);
  finally
    Lines.Free;
  end;
end;

And it started wrapping the string as expected:

image

I'm wondering why is this happening, and how should I do for using the WrapText() function with such strings?


Solution

  • What you describe is intentional behavior.

    In Delphi XE and earlier, the WrapText() documentation included this statement:

    WrapText does not insert a break into an embedded quoted string (both single quotation marks and double quotation marks are supported).

    In Delphi XE2 onwards, that statement is omitted from the documentation, but the behavior is still implemented in the RTL.

    I have opened a ticket with Embarcadero about this omission:

    RSP-24114: Important clause about embedded quoted strings is missing from WrapText documentation