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:

DonManfred

Expert
Licensed User
Longtime User
As the output is HTML you just can use <br /> to create a new line

To output the date you can use dateutils methods inside the literal

Assuming you have a string datestr which you want to insert.

Use ${datestr}
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim dstart As Long = DateTime.DateTimeParse("01.5.2015","00:00")
    log(DateTime.Date(dstart))
will log the date.... To use this in the literal you can use it like this
B4X:
Log($"The date is ${DateTime.Date(dstart)}"$)

this can be done in you
INITIAL_TEXT = $"html code..."$
too
 

Ed Brown

Active Member
Licensed User
Longtime User
Man! I wish I had seen this post for my last project! Smart Strings would have been a very handy feature.

Question: Are there any rules to Smart Strings in relation to obfuscation in Process_Globals? ie. Does obfuscation still work and do the same rules apply?
 

stari

Active Member
Licensed User
Longtime User
hi
i wish to send string:
{
"$token": "36ada5b10da39a1347559321baf13063",
}
but i can't include $.
Any seuggestion ?
 

stari

Active Member
Licensed User
Longtime User
First option:
B4X:
Dim s As String = $"
{
"${"$"}token": "36ada5b10da39a1347559321baf13063",
}
   "$

Better option:
B4X:
Dim jg As JSONGenerator
jg.Initialize(CreateMap("$token": "36ada5b10da39a1347559321baf13063"))
Log(jg.ToPrettyString(4))
thks
 

stari

Active Member
Licensed User
Longtime User
And the best way for this :
{
"$token": "36ada5b10da39a1347559321baf13063",
"$distinct_id": "13793",
"$ip": "123.123.123.123",
"$set": {
"Address": "1313 Mockingbird Lane"
}
}
 

johndb

Active Member
Licensed User
Longtime User
Syntax checking is not ignored within multi-line smart strings.

The following single line smart string statement works:
B4X:
dim s as string = $"Sub xyz(var1, var2)"$

The following multi-line smart string statement fails as B4X thinks that it is code:
B4X:
dim s as string =  _
$"
sub xyz(var1, var2)
"$

All text within the multi-line string should not be checked at all.

Is this a bug?

[SOLVED] You must start a line with the $" and text and the last line must end with "$ on the same lines:
B4X:
dim s as string = _
$"Sub xyz (var1 as int, var2 as int) as int
return var1+var2
end sub"$

Thanks,

John
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
It took me a while to figure out this convoluted way, although someone else may find a much simpler string literal and make a fool out of my solution.
B4X:
Dim s As String = $"Sub${Chr(32 _
    )}xyz(var1, var2)
    "$
    Log(s)     'Displays: Sub xyz(var1, var2)
 

avalle

Active Member
Licensed User
Longtime User
Does a Smart String help dealing with long strings which should not contain CRLF?
Currently I'm dealing with them as such:

B4X:
resp.Write($"info="my long string...","$ & _
$"descr="my other long string","$ & _
$"etc etc..."$)

I would like to do it as such:

B4X:
resp.Write($"info="my long string...", & _
descr="my other long string", & _
etc etc..."$)

Any chance? Thanks for your advice.
 

JesseW

Active Member
Licensed User
Longtime User
If you need to use a string with a quote inside then escape it with double quotes:
in your CSVParser (which I greatly appreciate btw...) there is a line
B4X:
word = word.Replace(QUOTE, $""""$)
if I'm understanding smart strings correctly, this could have also been writen as
B4X:
word = word.Replace(QUOTE, """""")
is this correct?
 
Top