Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

[Yukon Beta Blog] Delphi Language: Modernizing String Literals

rad12betastringliterals

This blog post is based on a pre-release version of the RAD Studio software and it has been written with specific permission by Embarcadero. No feature is committed until the product GA release.

As with most other programming languages, Delphi’s Object Pascal allows a developer to use string literals, that is a string value written directly in the code. This is very common, even if not always recommended; it is better to use a  resource string since they offer advantages in terms of memory management and translation support for string to show to the user. 

Now string literals in Delphi have been tied since the early days to the original Turbo Pascal string type, which is the type we refer to today as “short strings”. Short strings have a maximum of 255 characters and are allocated directly as a static array. Delphi introduced dynamically allocated, long strings starting with Delphi 2. Initially long strings were based on Ansi, and later on Unicode. However, string literals never made the same transition from the early days, and they have been limited to the short string type, and retained  a maximum of 255 characters.

The 12.0 version of Delphi (and RAD Studio) introduces a very significant set of changes to the way the Delphi compiler handles string literals.

Long String Literals

With the coming release, it will be possible to have string literals longer than 255 characters. There is no specific limitation in the compiler. However, the length of a literal string might still be constrained by the editor’s limitations of 4K characters per line. There is no change in syntax; you can simply have a literal string longer than 255 chars.

120 strings 1

Above: A 600+ characters long string literal in the editor.

Multiline Strings

What’s more relevant is the second extension to string literals is Delphi 12 will add support for multiline strings. In this case, you have to adopt a different syntax:  multiline strings are delimited by a triple quote (’’’).

There are a few more syntax rules for a multiline string :

  • It is introduced by a triple quote (’’’) and a new line (so no text following the triple quotes on the same line)
  • It can encompass multiple lines of the source code file (with no limitation)
  • It ends with a closing triple quote (’’’) in a line without any text preceding it

What’s significant, compared to the past, is that there is no need to concatenate the lines into single-line strings with a + sign. Here is an example:

120 strings 2

Above: A multiline string literal in the editor.

Update: What’s the formatting of the string above? Bonus feature: TEXTBLOCK directive

Following questions after publishing the blog post, let me clarify the strJSON string content. The initial and file new lines are part of the syntax and not included. There are newlines for each line. For those you can use a new directive defined as:

{$TEXTBLOCK NATIVE/CR/LF/CRLF [<ident>]}

In this directive you can specify how the line breaks in the multi line strings are treated. The additional ident parameter is a decoration, ignored by the compiler but used by external tools. It may be HTML, XML, JSON, SQL, etc to indicate the content and possibly drive a syntax highlighting tool.

Last element is the indentation. As I wrote the closing triple quote position determines the “ignored indentation”. So in this case there will be two spaces for each of the three indented lines and no indentation for the first and the last line.

Further update: What about having a triple quote in a multiline string?

Notice that the triple quotes (”’) can also be replaced with a large odd number of quotes, like 5 or 7. This allows embedding an actual triple quote within a multiline string. Example:

Want to try now?

These features are enabled in the current Yukon beta, available to customers with an active update subscription. If you are in that group and haven’t received an invite, you can contact an Embarcadero sales rep or sales partner and ask for the signup link.

If you don’t have an active subscription, you can consider buying a new license or contacting sales, and once you have a license you can ask  to join the beta.

This blog post is based on a pre-release version of the RAD Studio software and it has been written with specific permission by Embarcadero. No feature is committed until the product GA release.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Marco is one of the RAD Studio Product Managers, focused on Delphi. He's the best selling author of over 20 books on Delphi.

29 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES