9

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>';

1 Answer 1

15

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';
7
  • 1
    Thanks, appreciate that answer... however this is sad sad news Jan 11, 2016 at 16:39
  • How is it sad? Result := 'foo' + 'bar'; will be done at compile time, so it will result in one single constant, 'foobar'. I have coded very long constant strings this way, and they do not take up any runtime. They are just there, compiled in as static text. Your way, Result := 'foo'; Result := Result + 'bar'; is slow, because it concatenates at runtime.. Jan 11, 2016 at 17:06
  • 2
    @RudyVelthuis Max is sad because he does not want to have to concatenate. He'd like to use a multi-line literal. Jan 11, 2016 at 17:33
  • 1
    Max, think of this as multiline-literal syntax that is easy to read, albeit harder to write. . Are you happy now? If you would like to automatically turn a lot of literal text into this syntax, it would be easy to write a little tool to "paste multi-line syntax literal".
    – Warren P
    Jan 11, 2016 at 21:46
  • 1
    Yes I'm sad because I wanted to use a multi line literal for ease of writing, I went for your solution in the end because it is the best given the limitations of the technology, yes you're right maybe I could read it in from a file if it was a lot of text :) Jan 12, 2016 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.