Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a multiple line verbatim string in Delphi/Pascal

Tags:

string

delphi

In c# you can use multiline literal strings to have a string which spans a physical line break in the sourcecode e.g.

var someHtml = @"<table width="100%" border="0" cellspacing="0" cellpadding="5" align="center" class="txsbody">
    <tbody>
        <tr>
            <td width="15%" class="ttxb">&nbsp;</td>
            <td width="85%" class="ttxb"><b>COMPANY NAME</b></td>
        </tr>
    </tbody>
</table>";

but how to do this in delphi without using string concatenation, not so much for performance but for looking visually as nice as in c# instead of

Result :        = '<table width="100%" border="0" cellspacing="0" cellpadding="5" align="center" class="txsbody">';
Result : Result + '<tbody>';
like image 861
Max Carroll Avatar asked Jan 11 '16 16:01

Max Carroll


People also ask

How do you type a multiline string?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

Which characters are used for multiline strings?

Three single quotes, three double quotes, brackets, and backslash can be used to create multiline strings.

Can double quote strings span multiple lines?

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. Not only does this allow multiple lines, but it also turns off escaping.

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.


1 Answers

How to do this in delphi without using string concatenation?

You cannot. There is no support for multi-line literals. Concatenation is the only option.

However, your Delphi code performs the concatenation at runtime. It's far better to do it at compile time. So instead of:

Result := 'foo';
Result := Result + 'bar';

write

Result := 'foo' +
          'bar';
like image 78
David Heffernan Avatar answered Oct 15 '22 06:10

David Heffernan