Brendan
Brendan

Reputation: 19353

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

I want to create a string that spans multiple lines to assign to a Label's Caption property.

How is this done in Delphi?

Upvotes: 111

Views: 209072

Answers (13)

Wendigo
Wendigo

Reputation: 59

The platform agnostic way would be to use sLineBreak:

Write('Hello' + sLineBreak + 'World!');

Upvotes: 5

quasoft
quasoft

Reputation: 5438

Since RAD Studio 12.0 (end of 2023) Delphi supports Multiline String Literals.

Use 3 (or more) single quotes ''' at the start and end, and you get a multiline literal:

 const
    str = '''
      [
        {"id" : "1", "name" : "Name1"},
        {"id" : "2", "name" : "Name2"},
        {"id" : "3", "name" : "Name3"}
      ]
    ''';

The documentation explains how white space is treated:

Multiline string indentation and formatting logic are used very specifically. A multiline string treats a leading white space this way:

  • The closing ''' needs to be in a line of its own, not at the end of the last line of the string itself.
  • The indentation of the closing ''' determines the base indentation of the entire string.
  • Each blank space before that indentation level is removed in the final string for each of the lines.
  • None of the lines can be less indented than the base indentation (the closing '''). This is a compiler error, showing also as Error Insight.
  • The last newline before the closing ''' is omitted. If you want to have a final new line, you should add an empty line at the end.

And there is also a TEXTBLOCK directive that can be used to specify how whitespace is treated.

Upvotes: 2

Samuel Cruz
Samuel Cruz

Reputation: 51

You have the const sLineBreak in the System.pas unit that already does the treatment according to the OS you are working on.

Example of use:

TForm1.btnInfoClick(Sender: TObject);
begin
   ShowMessage ('My name is Jhon' + sLineBreak
      'Profession: Hollywood actor');
end;

Upvotes: 2

boodyman28
boodyman28

Reputation: 1

 private
   { Private declarations }
   {declare a variable like this}
   NewLine : string; // ok
  // in next event handler assign a value to that variable (NewLine)
  // like the code down
procedure TMainForm.FormCreate(Sender: TObject);
begin`enter code here`
  NewLine := #10;
 {Next Code To show NewLine In action}
  //ShowMessage('Hello to programming with Delphi' + NewLine + 'Print New Lin now !!!!');
end;

Upvotes: -3

Dave Sonsalla
Dave Sonsalla

Reputation: 9

Sometimes I don't want to clutter up my code space, especially for a static label. To just have it defined with the form, enter the label text on the form, then right click anywhere on the same form. Choose "View as Text". You will now see all of the objects as designed, but as text only. Scroll down or search for your text. When you find it, edit the caption, so it looks something like:

Caption = 'Line 1'#13'Line 2'#13'Line 3'

#13 means an ordinal 13, or ascii for carriage return. Chr(13) is the same idea, CHR() changes the number to an ordinal type.

Note that there are no semi-colon's in this particular facet of Delphi, and "=" is used rather than ":=". The text for each line is enclosed in single quotes.

Once you are done, right-click once again and choose "View as Form". You can now do any formatting such as bold, right justify, etc. You just can't re-edit the text on the form or you will lose your line breaks.

I also use "View as Text" for multiple changes where I just want to scroll through and do replacements, etc. Quick.

Dave

Upvotes: -2

Jessé Catrinck
Jessé Catrinck

Reputation: 2277

var
  stlst: TStringList;
begin
  Label1.Caption := 'Hello,'+sLineBreak+'world!';

  Label2.Caption := 'Hello,'#13#10'world!';

  Label3.Caption := 'Hello,' + chr(13) + chr(10) + 'world!';

  stlst := TStringList.Create;
  stlst.Add('Hello,');
  stlst.Add('world!');
  Label4.Caption := stlst.Text;

  Label5.WordWrap := True; //Multi-line Caption
  Label5.Caption := 'Hello,'^M^J'world!';

  Label6.Caption := AdjustLineBreaks('Hello,'#10'world!');
  {http://delphi.about.com/library/rtl/blrtlAdjustLineBreaks.htm}
end;

Upvotes: 6

Toby Allen
Toby Allen

Reputation: 11213

I dont have a copy of Delphi to hand, but I'm fairly certain if you set the wordwrap property to true and the autosize property to false it should wrap any text you put it at the size you make the label. If you want to line break in a certain place then it might work if you set the above settings and paste from a text editor.

Hope this helps.

Upvotes: 0

SwallowIt
SwallowIt

Reputation:

ShowMessage('Hello'+Chr(10)+'World');

Upvotes: 1

Zartog
Zartog

Reputation: 1926

Here's an even shorter approach:

my_string := 'Hello,'#13#10' world!';

Upvotes: 43

Jim McKeeth
Jim McKeeth

Reputation: 38703

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.

Upvotes: 196

Brendan
Brendan

Reputation: 19353

my_string := 'Hello,' + #13#10 + 'world!';

#13#10 is the CR/LF characters in decimal

Upvotes: 22

skamradt
skamradt

Reputation: 15538

Or you can use the ^M+^J shortcut also. All a matter of preference. the "CTRL-CHAR" codes are translated by the compiler.

MyString := 'Hello,' + ^M + ^J + 'world!';

You can take the + away between the ^M and ^J, but then you will get a warning by the compiler (but it will still compile fine).

Upvotes: 12

Francesca
Francesca

Reputation: 21640

On the side, a trick that can be useful:
If you hold your multiple strings in a TStrings, you just have to use the Text property of the TStrings like in the following example.

Label1.Caption := Memo1.Lines.Text;

And you'll get your multi-line label...

Upvotes: 8

Related Questions