![Rad12BetaStringLiterals | Embarcadero RAD Studio Delphi C++Builder Blogs rad12betastringliterals](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2023/09/Rad12BetaStringLiterals-8605391.png?resize=1140%2C694&ssl=1)
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.
Table of Contents
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.
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:
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:
1 2 3 4 5 |
var s := ''''' some text and now ''' some more text '''''; |
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.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today
   Free Delphi Community Edition   Free C++Builder Community Edition
Great news! If you modify the code formatter to accommodate the new syntax, please include an option to exclude certain parts from formatting. Thanks!
Great news! If you modify the code formatter to accommodate the new syntax, please include an option to exclude certain parts from formatting. Thanks!
Change triple char (”’) to a single char (“).
That would differentiate simple string const from multiline one while not broke insertion of ‘ chars in string.
So how would that work in a string like this?
AString := 'I said "hello" to him. "Oh", he said "hello to you too"';
Just like today. Nothing different.
All of this is valid:
s:=”1423453453″;
s:=’1423453453′;
s:=”line1
line2 (‘test’)
Line3″;
Basically if you start string with ” you should end it with “.
if you start it with ‘ you should end it with “.
‘ and ” symbols inside constant would be added as ” or “”. Just like today.
Damn.
Edit:
if you start it with â you should end it with ‘.
I wish i could edit my posts.
This is valid too:
s:=â1423″453″453âČ;
And this:
s:=’line1
line2 (“test”)
Line3′;
Successfully installed Yukon in a Windows 11 VM.
What about string helpers?
https://app.screencast.com/Tw9D2mQqCBBXD
When I have multline constant
s :=
”’first line
second line
third line”’;
What chars will I get between the lines, #13#10 ?
Will this change to #10 if In convert my pas-file from Windows line endings to Unix line endings?
Will this be dependent on line endings of the source file? If such, that’s kind of a caveat, because converting line endings never seem to change source code behavior.
In terms of newlines configuration for multiline strings, I added an “updated” section to the blog post. There is a specific compiler directive for this.
Thanks for the answer, Marco! But I’m still a bit confused
Am I right, that
s := ”’
first line
second line
”’;
is equivalent to string ‘first linesecond line’, so that there are NO any symbols between ‘first line’ and ‘secord line’ ? So when I need real line ends in resulting string, I still need to write something like
s := ”’
first line#13#10
second line
”’
Right?
Hi Anton, whatever you are using as a normal line break works. The IDE takes care of it but normally it is CRLF. It is actually looking for the closing
` characters on a line of its own and will treat anything in between as it normally does.
Thanks, Ian! Could you please anwser directly, because I still dont’ catch this new semantics:
s:=”’
1
2
”’;
Means that
s[1]=’1′
s[2]=’2′
OR
s[1]=’1′
s[2]=#13
s[3]=#10
s[4]=’2′
?
Ah, I understand now! It is the second example. The string would contain
s := '1' + #13 + #10 + '2';
nots := '12';
Here’s a screenshot showing the results in Yukon:
Note that in my example there are ASC(32) – spaces – in front of the 1 and 2 characters. If I had put them at the start of the line of code those spaces would not be there.
When I write:
s := ”’
1
2
”’;
Is
s[1] = ‘1’
s[2] = ‘2’
OR
s[1] = ‘1’
s[2] = #13
s[3] = #10
s[4] = ‘2’
?
Has this been used in TWriter.WriteComponent? I would so like to see the back of the horrible string literals in DFM files
Hi Peter (waves), it’s a language feature so it’s more of a code thing rather than a methodology in the nuts and bolts of how the IDE/component streaming works. I understand what you mean by the string literals in DFM files.
Just it’ll good to see similar progress in a dfm-parser and generator.
Is there a release date for the official version
I’m afraid we do not have a current release date for the GA version.
I have an “Advanced Beta Access” in my subscription. How I can get Delphi 12 Beta?
Hi Nikolay. I checked the details you supplied with this comment, and I couldn’t find you list using them. You should contact your local sales representative if you’re not sure of your details. The invites would have been sent out in July to the email address used when registering and associated with your EDN account. It is possible the invite went to a spam filter so I would check there. The title of the email would be something like “Invitation to join the Yukon beta”. If you replied to that then you should be on the list and have received the beta invite acknowledgment link.
Please note that as of 11th Oct ’23 we are not accepting any new invites for the beta unless they have already signed up prior to that date.
That’s super cool feature. The only thing seriously missing now in Delphi dialect to be a modern language is a real ternary operator such as a := b ? c : d; {C syntax}
I’m glad you like the feature. The ternary operator is something quite a few people have mentioned, and it’s listed on our quality portal as a request. It’s really helpful if you upvote one of those requests – because that way we can see how much demand there is for the feature which helps a lot in making decisions about development effort in the future.
You can find the relevant reports with this search: https://quality.embarcadero.com/issues/?jql=text%20~%20%22ternary%22
Thank you Ian for bringing that up. I already voted for this, and sorted by votes descending it’s the #3 feature request.
https://quality.embarcadero.com/issues/?jql=order%20by%20votes%20desc
#1 Nameof() compiler’s magic – 190 votes
#2 RPI support – 170 votes
#3 Ternary operator – 135 votes but summing up all request for that it’s 149 votes
#4 Linux in in delphi Pro – 140 votes
So imho it is definitively something that your team should consider in a not too distant future.
Syntax highlight allow to add a background color for string literals. It would be nice, if it will look at Ident argument in mentioned directive to not highlight background inside ident’s spaces to more clear displaying of exclusion of this spaces from this string value. %)