Conversion Functions
ObjectScript provides several conversion functions. You use $Translate to translate one or more characters in a string into different characters. You can also use it to remove characters from a string. $Replace is similar to $Translate, except it treats its second and third arguments as complete strings, rather than lists of characters. For typical tasks such as converting between upper and lower case, or capitalizing words, ObjectScript also provides $ZConvert, and for removing whitespace, punctuation, and other types of characters, there is $ZStrip.
VS Code - ObjectScript
Class ObjectScript.Examples
{
ClassMethod Conversions()
{
write !, "abcde becomes: ", $translate("abcde", "ad", "yz") // translate a->y, and d->z
write !, "abcde becomes: ", $translate("abcde", "ad", "zz") // translate a->z, and d->z
write !, "abcde becomes: ", $translate("abcde", "ad", "z") // translate a->z, and d->nothing
write !, "abcdebcbc becomes: ", $translate("abcdebcbc", "abc", "yz") // translate a->y, b->z, and c->nothing
write !, "abcdebcbc becomes: ", $replace("abcdebcbc", "abc", "yz") // replace abc->yz
read !, "String to translate: ", x
set lower = "abcdefghijklmnopqrstuvwxyz"
set upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
write !, "Using $translate: ", $translate(x, lower, upper)
write !, "Using $zconvert: ", $zconvert(x, "U")
write !, "Using $zconvert for capitalizing words: ", $zconvert(x, "W")
write !, "Using $zstrip to remove whitespace: ", $zstrip(x, "*W")
}
}
Testing using the Terminal
USER>do ##class(ObjectScript.Examples).Conversions()
abcde becomes: ybcze
abcde becomes: zbcze
abcde becomes: zbce
abcdebcbc becomes: yzdezz
abcdebcbc becomes: yzdebcbc
String to translate: the quick brown fox jumps over the lazy dog
Using $translate: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Using $zconvert: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Using $zconvert for capitalizing words: The Quick Brown Fox Jumps Over The Lazy Dog
Using $zstrip to remove whitespace: thequickbrownfoxjumpsoverthelazydog
USER>