将SysUtils.WrapText()与包含单引号的字符串一起使用

时间:2019-03-29 17:38:51

标签: delphi escaping word-wrap delphi-2007

我正在尝试将SysUtils.WrapText()函数与包含转义单引号字符的字符串一起使用,并且得到了意外的结果。

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;

如果字符串包含撇号字符,则该函数似乎根本不会包装字符串。

我也尝试使用#39代码而不是单引号char,但是问题仍然存在。此外,我已经检查了Lines.Count,它是1

image

我尝试删除单引号字符:

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;

它开始按预期包装字符串:

image

我想知道为什么会发生这种情况,以及如何将WrapText()函数与这样的字符串一起使用?

2 个答案:

答案 0 :(得分:5)

您描述的是故意行为

在Delphi XE和更早的版本中,WrapText() documentation包含以下语句:

  

WrapText不会在嵌入的带引号的字符串中插入中断(支持单引号和双引号)。

从Delphi XE2开始,文档中省略了该语句,但该行为仍在RTL中实现。

我已就此疏漏向Embarcadero开了票:

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

答案 1 :(得分:1)

在10.3.1中,源代码包含用于处理双引号和单引号的引号字符的代码,它们看起来只是忽略了它们之间的文本。因此,一种解决方案是使用与单引号字符不同的撇号。第二将是避免使用收缩。函数源的开始:

function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet;
  MaxCol: Integer): string;
const
  QuoteChars = ['''', '"'];
  FirstIndex = Low(string);
  StrAdjust = 1 - Low(string);
var
...

一个选项:

    Lines.Text := WrapText('Can`t format message, message file not found', 15);

第二种选择:

    Lines.Text := WrapText('Cannot format message, message file not found', 15);
相关问题