Android Question Replace variables in string

Hi all, i want to replace dinamically variables in strings. Something like this

I recibe a string in a JSON and after the parse i obtain this:

B4X: Copy
JSONdata = "The New date is: g_date"

the app has the variable:
B4X: Copy
Dim g_date as String
g_date = "31/12/2022"

So i need when i put the JSONdata to a label atomaticly replace the string variable
B4X: Copy
label.text = (i dont know what format here)

'the output
log(label.text)
'The New date is: 31/12/2022

I need this why i want to format the strings outside the app (in server) but the data in variables are in the app

Thanks
 
Maybe
B4X: Copy
label.text = JSONdata.Replace("g_date", g_date)
?
 
Upvote 0
Maybe like that?
B4X: Copy
    Dim g_date As String = "31/12/2022"
    l.Text = "The New date is: g_date"
    l.Text = l.Text.Replace("g_date", g_date)
    Log(l.Text)

@DonManfred
Oops sorry too late ...
 
Upvote 0
The code should not be permanent. The replacement must be dynamic

I will not always receive the same variable/variables in the JSON
 
Last edited:
Upvote 0
You can concat strings:
B4X: Copy
label.text = "The New date is: " & g_date
 
Upvote 0
B4X: Copy
label.text = $"The New date is: ${g_date}"$

 
Upvote 0
I will not always receive the same variable/variables in the JSON

Is there a known set of variables that you might receive from your parsed JSON?

What happens if your parsed JSON refers to a variable that isn't present in your program?

What happens if you receive "The New date is: big_date" and you have a variable g_date = "16/10/2022"?
ie are you expecting "The New date is: bi16/10/2022"?
 
Upvote 0
B4X: Copy
label.text = $"The New date is: ${g_date}"$

i tried it but cant figure out how make work if this string: The New date is: ${g_date} i get from a JSON

Is there a known set of variables that you might receive from your parsed JSON?

What happens if your parsed JSON refers to a variable that isn't present in your program?

What happens if you receive "The New date is: big_date" and you have a variable g_date = "16/10/2022"?
ie are you expecting "The New date is: bi16/10/2022"?
i know all the variables so i avoid that mistake

In addition i say that this is a little example, i need this why i need change the format of the strings dinamically but with data in variables.
 
Upvote 0
i tried it but cant figure out how make work if this string: The New date is: ${g_date} i get from a JSON
Replacing a string variable using Smart String Literal as pointed by @calloatti is the recommended way.

You may wrap the variable with your own template like %g_date% or $g_date$
Other option is to use regex.
 
Upvote 0
Something like this might do what you're looking for:

B4X: Copy
Do While CheckMoreJSONdataExists()
    Dim JSONdata As String = GetNextJSONdata()
  
    'sample test data
    Dim JSONdata As String = "The New date is: g_date and it is a g_dayofweek"
  
    Log("This: [" & JSONdata & "]")

    If JSONdata.Contains("g_username")  Then JSONdata = JSONdata.Replace("g_username",  GetCurrentUserName())
    If JSONdata.Contains("g_password")  Then JSONdata = JSONdata.Replace("g_password",  GetCurrentPassword())
    If JSONdata.Contains("g_date")      Then JSONdata = JSONdata.Replace("g_date",      GetDateInLocalFormat())
    If JSONdata.Contains("g_time")      Then JSONdata = JSONdata.Replace("g_time",      GetTimeInLocalFormat())
    If JSONdata.Contains("g_dayofweek") Then JSONdata = JSONdata.Replace("g_dayofweek", GetDayOfWeek())

    'or if you're not worried about efficiency and/or the substitution variables are already set then:

    JSONdata = JSONdata.Replace("g_username",  g_username)
    JSONdata = JSONdata.Replace("g_password",  g_password)
    JSONdata = JSONdata.Replace("g_date",      g_date)
    JSONdata = JSONdata.Replace("g_time",      g_time)
    JSONdata = JSONdata.Replace("g_dayofweek", g_day_of_week)    'variable name doesn't have to match JSON name
    JSONdata = JSONdata.Replace("$moonphase$", g_phase_of_moon)    'variable name can even be entirely different
  
    Log("Becomes this: [" & JSONdata & "]")
Loop
 
Last edited:
Upvote 0
I thought Smart String Literals only work when the literal string is known at compile time.

The original post looks like the "master" string is being parsed from dynamic JSON.
yes, string utils dont work , maybe if we had something like the ' or ` to diference that cases...

Something like this might do what you're looking for:

B4X: Copy
Do While CheckMoreJSONdataExists()
    Dim JSONdata As String = GetNextJSONdata()
 
    'sample test data
    Dim JSONdata As String = "The New date is: g_date and it is a g_dayofweek"
 
    Log("This: [" & JSONdata & "]")

    If JSONdata.Contains("g_username")  Then JSONdata = JSONdata.Replace("g_username",  GetCurrentUserName())
    If JSONdata.Contains("g_password")  Then JSONdata = JSONdata.Replace("g_password",  GetCurrentPassword())
    If JSONdata.Contains("g_date")      Then JSONdata = JSONdata.Replace("g_date",      GetDateInLocalFormat())
    If JSONdata.Contains("g_time")      Then JSONdata = JSONdata.Replace("g_time",      GetTimeInLocalFormat())
    If JSONdata.Contains("g_dayofweek") Then JSONdata = JSONdata.Replace("g_dayofweek", GetDayOfWeek())

    'or if you're not worried about efficiency and/or the substitution variables are already set then:

    JSONdata = JSONdata.Replace("g_username",  g_username)
    JSONdata = JSONdata.Replace("g_password",  g_password)
    JSONdata = JSONdata.Replace("g_date",      g_date)
    JSONdata = JSONdata.Replace("g_time",      g_time)
    JSONdata = JSONdata.Replace("g_dayofweek", g_day_of_week)    'variable name doesn't have to match JSON name
    JSONdata = JSONdata.Replace("$moonphase$", g_phase_of_moon)    'variable name can even be entirely different
 
    Log("Becomes this: [" & JSONdata & "]")
Loop
this is the most aprox to i need... but i need to use cs builder to...

well i try to do the best...

Thanks all.
 
Upvote 0
Back
Top