Reputation: 18171
I have a string that contains Linux style line breaks. Linux style is #13
while Windows style is #13#10
.
I would like to show this string in a TMemo
. Looks like TMemo
accepts only Windows style and does not treat #13
as a new line.
Is the only way for TMemo
to format new lines is to insert #10
, or can I somehow ask TMemo
to act in Linux style?
Upvotes: 0
Views: 218
Reputation: 5381
Try this
const lf = chr(13) + chr(10);
var ls : string; // Linux Style String
lfs : string; // Windows Style String
begin
lfs := stringreplace(ls,#13,lf,[rfReplaceAll, rfIgnoreCase]);
end;
Upvotes: 1
Reputation: 596592
I would like to show this string in Memo. Looks like Memo accepts only Windows style and does not treat #$13 as new line.
That depends on how you give the string
to the Memo.
The underlying Win32 EDIT
control that TMemo
wraps only supports #13#10
style line breaks internally.
If you assign the string
to the TMemo.Text
property, it will just pass the string
as-is to the underlying Win32 control. So, the string
will need to use Windows-style line breaks only.
However, if you assign the string
to the TMemo.Lines.Text
property instead, it will internally adjust all styles of line breaks to Windows-style, and then give the adjusted string
to the Win32 control. So, in that regard, you can handle Linux-style and Windows-style line breaks equally.
Alternatively, the TStringList
class supports parsing all styles of line breaks (when its LineBreak
property matches the sLineBreak
constant, which it does by default). So, another option would be to first assign the string
to the TStringList.Text
property, and then you can assign the resulting list to the TMemo.Lines
property.
Upvotes: 3
Reputation: 8243
Actually Linux style is #10, not #13 (#13 is MacOS style, AFAIK). Also, note that it's #10 and not #$10 (which is #16).
The easiest way would be to replace the line ends on load/save, ie. instead of
Memo.Lines.LoadFromFile(FileName)
or
Memo.Lines.Text := STR;
do
uses System.IOUtils;
Memo.Lines.Text := TFile.ReadAllText(FileName,TEncoding.UTF8).Replace(#13#10,#13).Replace(#10,#13).Replace(#13,#13#10)
or
Memo.Lines.Text := STR.Replace(#13#10,#13).Replace(#10,#13).Replace(#13,#13#10)
and instead of
Memo.Lines.SaveToFile(FileName)
or
STR := Memo.Lines.Text
do
uses System.IOUtils;
TFile.WriteAllText(Memo.Lines.Text.Replace(#13#10,#13),TEncoding.UTF8)
or
STR := Memo.Lines.Text.Replace(#13#10,#13)
Of course, you should replace the TEncoding.UTF8 with the appropriate encoding you want to use.
Upvotes: 0