Android Tutorial [B4X] Smart String Literal

DonManfred

Expert
Licensed 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
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
hi
i wish to send string:
{
"$token": "36ada5b10da39a1347559321baf13063",
}
but i can't include $.
Any seuggestion ?
 

stari

Active Member
Licensed 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
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
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

Well Known Member
Licensed 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
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.
 
Top