Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a newline character in a string in Delphi?

Tags:

newline

delphi

I want to create a string that spans multiple lines to assign to a Label Caption property. How is this done in Delphi?

like image 715
Brendan Avatar asked Oct 31 '08 18:10

Brendan


People also ask

How do you add a newline to a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do I start a new line in Delphi?

So if you want to make your TLabel wrap, make sure AutoSize is set to true, and then use the following code: label1. Caption := 'Line one'+sLineBreak+'Line two'; Works in all versions of Delphi since sLineBreak was introduced, which I believe was Delphi 6.

Does \n work in a string?

in a string prevents actually making a new line and instead of Type (new line) for a new line it is Type \n for a new line .

Which character is used to get the new line?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.


2 Answers

In the System.pas (which automatically gets used) the following is defined:

const   sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF}                 {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF}; 

This is from Delphi 2009 (notice the use of AnsiChar and AnsiString). (Line wrap added by me.)

So if you want to make your TLabel wrap, make sure AutoSize is set to true, and then use the following code:

label1.Caption := 'Line one'+sLineBreak+'Line two'; 

Works in all versions of Delphi since sLineBreak was introduced, which I believe was Delphi 6.

like image 119
Jim McKeeth Avatar answered Sep 18 '22 19:09

Jim McKeeth


Here's an even shorter approach:

my_string := 'Hello,'#13#10' world!'; 
like image 26
Zartog Avatar answered Sep 21 '22 19:09

Zartog