Delphi TStringList和SaveToFile:当字符串结束时,我如何判断是否要换行?

Delphi TStringList和SaveToFile:当字符串结束时,我如何判断是否要换行?,delphi,delphi-xe2,Delphi,Delphi Xe2,我正在使用TStringList和SaveToFile。当字符串结束时,我如何判断是否要换行? 通常,TStringList中包含的所有字符串仅保存在一行中。当完成字符串并需要将其他字符串放入新行时,如何告诉列表放置回车符 字符串的格式为: 'my text....' + #10#13 您可以添加(或插入)空行: MyStringList.Add(''); MyStringList.SaveToFile(...); 您可以添加(或插入)空行: MyStringList.Add(''); My

我正在使用
TStringList
SaveToFile
。当字符串结束时,我如何判断是否要换行? 通常,
TStringList
中包含的所有字符串仅保存在一行中。当完成字符串并需要将其他字符串放入新行时,如何告诉列表放置回车符

字符串的格式为:

'my text....' + #10#13
您可以添加(或插入)空行:

MyStringList.Add('');
MyStringList.SaveToFile(...);
您可以添加(或插入)空行:

MyStringList.Add('');
MyStringList.SaveToFile(...);

如果像上面用
“我的文本…”“+#10#13+“其他文本…”
编写字符串,那么问题是您的行尾字符颠倒了。在Windows上,它们应该是
#13#10
(或者简单地使用
sLineBreak
常量)

下面是一个快速应用程序(Delphi XE2),它演示了错误的配对顺序将导致问题,以及解决问题的方法:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    SL.Add('This is a test string' + #10#13 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPair.txt');

    SL.Clear;
    SL.Add('This is a test string'+ #13#10 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPairFix.txt');
  finally
    SL.Free;
  end;
end.
第一个在记事本中打开时,生成:

This is a test stringThis is another test string
第二点:

This is a test string
This is another test string

如果像上面用
“我的文本…”“+#10#13+“其他文本…”
编写字符串,那么问题是您的行尾字符颠倒了。在Windows上,它们应该是
#13#10
(或者简单地使用
sLineBreak
常量)

下面是一个快速应用程序(Delphi XE2),它演示了错误的配对顺序将导致问题,以及解决问题的方法:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    SL.Add('This is a test string' + #10#13 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPair.txt');

    SL.Clear;
    SL.Add('This is a test string'+ #13#10 + 'This is another test string');
    SL.SaveToFile('C:\Test\BadLFPairFix.txt');
  finally
    SL.Free;
  end;
end.
第一个在记事本中打开时,生成:

This is a test stringThis is another test string
第二点:

This is a test string
This is another test string

将2个字符串添加到字符串列表中,SaveToFile将发出一个文件,每个字符串一行。我质疑你问题的前提。换行符序列是
#13#10
@andreas,因为它在这个x平台上是滑动的era@David:我仍然处于Win32时代…sLineBreak已经存在很长时间了,大概是在X-Platform(Kylix)的第一次尝试之后将2个字符串添加到字符串列表中,SaveToFile将发出一个文件,每个字符串一行。我质疑你问题的前提。换行符序列是
#13#10
@andreas,因为它在这个x平台上是滑动的era@David:我仍然处于Win32时代…sLineBreak已经存在很长时间了,大概是在X-Platform(Kylix)的第一次尝试之后