Replace text by args
Hi all,
I'm wondering if exists any command or method to replace a text using parameters.
In C# I use the Format property
var text = string.format("My name is {0} and I'm {1} years","Kurro","18")
// The value of text will be "My name is Kurro and I'm 18 years"
JavaJava
I've tried with this code... but it works only for the specific parameters
set text = "My name is {0}, and I'm {1} years"
write $Replace($Replace(text,"{0}","Kurro"),"{1}",18)
ObjectScriptObjectScript
is possible to do something for more args? I mean, use for a indeterminate number of args
Best regads
Product version: IRIS 2022.1
$ZV: IRIS for Windows (x86-64) 2021.1 (Build 215U) Wed Jun 9 2021 09:39:22 EDT
Hi Kurro!
I like using this feature of COS:
write "my string", stringVariable, "continue the string"
it works fine for the WRITE calls, but if you need the string formatting in any other cases please specify them so we can find an easy way to do it!
PS.: it also works if you concatenate strings:
write "my string"_stringVariable_"continue string"
In the first case you would be calling the "WRITE" method 3 times, once for each string divided by ",", and in the second you would be calling "WRITE" once with only one string that is a copy of the 3 strings combined together.
You could also use concatenating to set a new variable:
Set stringVariable = " | " Set stringsCombined = "my string"_stringVariable_"continue the string"
And then you can work with this new variable.
I.E. if you called WRITE stringsCombined, you would have as an output:
Hey Kurro.
I'm not sure of a built in function for this, but if you wanted to have your own:
Class Demo.FunctionSets.Example { ClassMethod Format(InputString As %String, Params... As %String) As %String { Set OutputString = InputString For i = 1 : 1 : $GET(Params, 0){ Set OutputString = $Replace(OutputString,"{"_i_"}",Params(i)) } Quit OutputString } }
And then:
Thanks for the solution.
I'm using several templates of texts so It will be essential in my future developments
There is:
##class(%MessageDictionary).FormatText("Hello %1", "World")
Also "Include %occMessages" at top of your class will give access to macros:
* FormatText
* FormatTextHTML
* FormatTextJS
Both approaches work for the indeterminate number of args requirement.
The standard approach for this in ObjectScript is the $$$FormatText macro - for example:
Results in:
I'll add - this is particularly helpful in conjunction with localization and used heavily in IRIS' own localization of e.g. error messages.
It's great to see that there's a built in macro for this!
Hi Timothy,
Thank you so much for your response and for explaining the use of the $$$FormatText macro in ObjectScript. It's great to know that this macro simplifies the process of string formatting and can be particularly useful in conjunction with localization. 🙌
Best regards!
While $$$FormatText() is a great tool, I'd give my left ... uh, leg to have a sprintf() workalike that handles left/right justification, padding, number precision formatting, date element tokens, etc.
I guess we'll just have to make do with Python f-strings ...
💡 This question is considered a Key Question. More details here.