TStringList和SaveToFile:如何在字符串完成后告诉新行?

时间:2011-11-07 13:34:28

标签: delphi delphi-xe2

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

字符串的格式为:

'my text....' + #10#13

2 个答案:

答案 0 :(得分:4)

您可以添加(或插入)空行:

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

答案 1 :(得分:2)

如果您正在使用'my text....' + #10#13 + 'other text...'编写上面显示的字符串,则问题在于您的行结束字符已反转。在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
相关问题