Written by

Project Manager & Head of Interoperability at Salutic Soluciones, S.L.
Question Kurro Lopez · Apr 19, 2023

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"

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)

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

Comments

Heloisa Paiva · Apr 19, 2023

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! 

0
Heloisa Paiva  Apr 19, 2023 to Heloisa Paiva

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:

my string | continue the string
0
Julian Matthews · Apr 19, 2023

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:

Write ##Class(Demo.FunctionSets.example).Format("My name is {1} and I'm {2} years","Kurro","18")My name is Kurro and I'm 18 years
0
Kurro Lopez  Apr 19, 2023 to Julian Matthews

Thanks for the solution.

I'm using several templates of texts so It will be essential in my future developments

0
Alex Woodhead · Apr 19, 2023

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.

0
Timothy Leavitt · Apr 19, 2023

The standard approach for this in ObjectScript is the $$$FormatText macro - for example:

Class Demo.Text
{

ClassMethod Sample()
{
    Write $$$FormatText("Watch out %1, it's a %2!","Superman","large pizza made of Kryptonite")
}

}

Results in:

d ##class(Demo.Text).Sample()Watch out Superman, it's a large pizza made of Kryptonite!
0
Timothy Leavitt  Apr 19, 2023 to Timothy Leavitt

I'll add - this is particularly helpful in conjunction with localization and used heavily in IRIS' own localization of e.g. error messages.

0
Julian Matthews  Apr 20, 2023 to Timothy Leavitt

It's great to see that there's a built in macro for this!

0
Yone Moreno  Apr 20, 2023 to Timothy Leavitt

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!

0
Jeffrey Drumm  Apr 20, 2023 to Timothy Leavitt

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 ... wink

0