PxPlus User Forum

Twitter Twitter Twitter

Author Topic: string interpolation  (Read 259 times)

Thomas Bock

  • Diamond Member
  • *****
  • Posts: 170
    • View Profile
string interpolation
« on: November 15, 2023, 08:45:15 AM »
In C# there is the possibility to build strings by referencing/including variables.

Code: [Select]
string name = "Tom";
var date = DateTime.Now;
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");

These strings are much more readable than coding a concatenation of several small strings. E.g. building a SQL-statement becomes more straight forward.
If PxPlus would provide such functionality, we could gain speed, because the interpreter would not need to translate several lines of code.

Devon Austen

  • Administrator
  • Diamond Member
  • *****
  • Posts: 368
  • Don’t Panic
    • View Profile
    • PVX Plus Technologies
Re: string interpolation
« Reply #1 on: November 15, 2023, 10:16:13 AM »
Here is an example of your code in PxPlus:

Code: [Select]
name$ = "Tom"
print "Hello, "+name$+"! Today is "+dte(0:"%Wl")+", it's "+dte(0:"%Hz:%mz")+" now."

Concatenation can happen on the same line of code.

As for speed I don't think the $"xxxx {var} xxxx" style string building would be any faster from a performance standpoint. Internally the same thing will be happening building the string plus now you have to scan the string for the variables as well.

Does that syntax work for you?
Software Developer for PVX Plus Technologies LTD.

Allen Miglore

  • Silver Member
  • ***
  • Posts: 37
    • View Profile
    • UnForm
Re: string interpolation
« Reply #2 on: November 15, 2023, 10:20:17 AM »
I agree, this is a handy feature of many languages.  I wrote a function to do something similar a while back and use it internally in UnForm.

def fn%resolveexpr$(local s$)
   local x,expr$,before$,after$,val$,between$
   while x>0
   return s$
end def

It works with anything that can be resolved with evs().

a=123, b$="456"

?fn%resolveexpr$("This is a: {str(a:""###0.00"")}, this is b$: {b$}")
This is a:  123.00, this is b$: 456

Internal braces can be escaped:

?fn%resolveexpr$("This is a: {str(a:""###0.00"")}, this is b$: \{b$\}")
This is a:  123.00, this is b$: {b$}

Stéphane Devouard

  • Diamond Member
  • *****
  • Posts: 119
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Re: string interpolation
« Reply #3 on: November 16, 2023, 06:55:57 AM »

String interpolation exists (kinda) in PxPlus :

Code: [Select]
print MSG(="Hello, %1 ! Today is %2, it's %3 now.",name$,dte(0:"%Wl"),dte(0:"%Hz:%mz"))
Stéphane Devouard
Portfolio | Work