Android Tutorial [B4X] Smart String Literal

The "smart string" literal is a more powerful version of the standard string literal.
It has three advantages:
  1. Supports multi-line strings.
  2. No need to escape quotes.
  3. Supports string interpolation.
The smart string literal starts with $" and ends with "$.
Examples:
B4X:
Dim s As String = $"Hello world"$
Dim query As String = $"
SELECT value_id FROM table3
WHERE rowid >= random()%(SELECT max(rowid)FROM table3)
AND second_value ISNOTNULL
LIMIT 1"$
Log($"No need to escape "quotes"! "$)

String Interpolation

Smart strings can hold zero or more placeholders with code. The placeholders can be easily formatted.
A placeholder starts with $[optional formatter]{ and ends with }:
B4X:
Log($"5 * 3 = ${5 * 3}"$) '5 * 3 = 15
You can put any code you like inside the placeholders.
B4X:
Dim x = 1, y = 2, z = 4 As Int
Log($"x = ${x}, y = ${y}, z = ${Sin(z)}"$) 'x = 1, y = 2, z = -0.7568024953079282

This is a compile time feature. You cannot load the strings from a file for example.

Number Formatter

The number formatter allows you to set the minimum number of integers and the maximum number of fractions digits. It is similar to NumberFormat keyword.

The number formatter structure: MinIntegers.MaxFractions. MaxFractions component is optional.
Examples:
B4X:
Dim h = 2, m = 15, s = 7 As Int
Log($"Remaining time $2{h}:$2{m}:$2{s}"$) 'Remaining time 02:15:07
Log($"10 / 7 = $0.3{10 / 7}"$) '10 / 7 = 1.429
Log($"$1.2{"The value is not a number!"}"$) 'NaN

Other Formatters

Note that the formatters are case insensitive.
Date - Equivalent to DateTime.Date:
B4X:
Log($"Current date is $date{DateTime.Now}"$) 'Current date is 02/02/2015

Time - Equivalent to DateTime.Time:
B4X:
Log($"Current time is $time{DateTime.Now}"$) 'Current time is 11:17:45

DateTime - Equivalent to DateTime.Date & " " & DateTime.Time:
B4X:
Log($"Current time is $DateTime{DateTime.Now}"$) 'Current time is 02/02/2015 11:18:36

XML - Escapes the five XML entities (", ', <, >, &):
B4X:
Dim UserString As String = $"will it break your parser ><'"&?"$
Log($"User input is: $xml{UserString}"$)
'User input is: will it break your parser &gt;&lt;&#39;&quot;&amp;?
This is also useful for html content.
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
I imagine that the 'Smart String Literal' is faster in terms of performance than StringBuilder isn't t? I'm just wondering if I should replace all my stringbuilders with smart string literals. I use stringbuilder to generate my MySQL queries all the time.
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Opps :oops::D
Thanks DonManfred.. I'm Mike Wazowski :D
Peluche-Mike-Wasausky-De-La-Pelicula-Monter-Inc-20140928224021.jpg
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I imagine that the 'Smart String Literal' is faster in terms of performance than StringBuilder isn't t? I'm just wondering if I should replace all my stringbuilders with smart string literals. I use stringbuilder to generate my MySQL queries all the time.
Don't. The performance will be the same.
 

Troberg

Well-Known Member
Licensed User
Longtime User
I imagine that the 'Smart String Literal' is faster in terms of performance than StringBuilder isn't t? I'm just wondering if I should replace all my stringbuilders with smart string literals. I use stringbuilder to generate my MySQL queries all the time.

Also, using a stringbuilder to build SQL strings is seldom worth the trouble. They are not very large (large memory allocations), and if you seldom build lots of them in a short time span (and if you do, it's probably better to optimize by trying to eliminate that.

Stringbuilders are excellent if you, for example, are reformatting a book in text format from line oriented text to paragraph oriented text, but for small strings, they don't give a noticeable gain.
 

jmon

Well-Known Member
Licensed User
Longtime User
Also, using a stringbuilder to build SQL strings is seldom worth the trouble. They are not very large (large memory allocations), and if you seldom build lots of them in a short time span (and if you do, it's probably better to optimize by trying to eliminate that.

Stringbuilders are excellent if you, for example, are reformatting a book in text format from line oriented text to paragraph oriented text, but for small strings, they don't give a noticeable gain.
Thanks for the reply

I thought that smart string literal would be faster because I the stringbuilder is a class and that I need to probably initialize some variables and methods internally. I am actually using SB a lot to have a split view of my Queries, like in the example 1 on the previous page. But as you said, I should probably just for normal strings.

I have tested Smart string literal for that, but there is one thing that I don't like, is that it respects the indentation and integrates it within the string:
B4X:
Sub WriteQuery
    If 1 = 1 Then
        Dim query As String = $"
        SELECT value_id FROM table3
        WHERE rowid >= random()%(SELECT max(rowid)FROM table3)
        AND second_value ISNOTNULL
        LIMIT 1"$
    End If   
    Log(query)
End Sub

Result:
B4X:
Program started.
        SELECT value_id FROM table3 'notice the indentation
        WHERE rowid >= random()%(SELECT max(rowid)FROM table3) 'notice the indentation
        AND second_value ISNOTNULL 'notice the indentation
        LIMIT 1 'notice the indentation

I guess that it should be like that. the smart string literal are probably just a bit too smart! :)
 

Troberg

Well-Known Member
Licensed User
Longtime User
I have tested Smart string literal for that, but there is one thing that I don't like, is that it respects the indentation and integrates it within the string:

(code snipped)

I guess that it should be like that. the smart string literal are probably just a bit too smart! :)

Maybe not too smart, but too literal... :)
 

Dave O

Well-Known Member
Licensed User
Longtime User
For multi-line strings, this is great, because I can enter a block of text that looks like how it will look at run time, like this:
B4X:
introText = $"
What's new:
- Fixed minor bugs.
- Added major bugs.
"$
However, this results in a leading and trailing CRLF. Is there a way to avoid these, while still being able to see a WYSIWYG block of text?

I could do this, of course:
B4X:
introText = $"What's new:
- Fixed minor bugs.
- Added major bugs."$
...but now it's harder to see the layout of the text, and (very slightly) harder to add another line of text at the bottom.
 

Dave O

Well-Known Member
Licensed User
Longtime User
Change "$ to "$.Trim

Ah, I should have thought of that. The smart string is just an object that you can then apply the Trim method to. Just tried it and it works great.

One minor glitch: The ".trim" text is colored as part of the string, instead of normal method coloring. I presume this is because the editor is doing its smart-string-literal coloring line by line?

Anyway, thanks!
 

Derek Johnson

Active Member
Licensed User
Longtime User
Just started using B4I and am missing Smart Quotes straight away! Amazingly handy for creating inline HTML, Javascript etc.

When can we expect them in B4I please?

Derek
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Just started using B4I and am missing Smart Quotes straight away!
1. Wrong forum (you should have posted it in B4I forum)
2. What are you missing? You want to use quotes in a smart string literal?
B4X:
Dim test As String = $""test""$$
 

Derek Johnson

Active Member
Licensed User
Longtime User
1. Wrong forum (you should have posted it in B4I forum)
2. What are you missing? You want to use quotes in a smart string literal?
B4X:
Dim test As String = $""test""$$

Yes - probably wrong forum, however Erel did say in the very first post of this topic:

This is a new feature that will be available in the next versions of B4A (4.30), B4J and B4i.

I was just asking when that release of B4I might be, as I find them incredibly useful in B4A.

Smart Strings in B4A are great. It's B4I that doesn't have them implemented yet.

BTW I think your example of a Smart String has one too many $ on the end.

Surely it should be:

B4X:
Dim test As String = $""test""$


Derek
 
Last edited:

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings and thank you in advance for your replies.

I used this code to insert smart string text into the HTMLEditor control:
B4X:
    Dim INITIAL_TEXT As String
    INITIAL_TEXT = $"<html><body>Lorem ipsum dolor sit
    amet, consectetur adipiscing elit. Nam tortor felis, pulvinar
    in scelerisque cursus, pulvinar at ante. Nulla consequat
    congue lectus in sodales. Nullam eu est a felis ornare
    bibendum et nec tellus. Vivamus non metus tempus augue auctor
    ornare. Duis pulvinar justo ac purus adipiscing pulvinar.
    Integer congue faucibus dapibus. Integer id nisl ut elit
    aliquam sagittis gravida eu dolor.
    Etiam sit amet ipsum </body></html>"$

How do insert CRLF and Today's date?

Sandy
 
Top