Caché ObjectScript Reference
$TRANSLATE
[Back] [Next]
   
Server:docs1
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Performs character-for-character replacement within a string.
Synopsis
$TRANSLATE(string,identifier,associator)
$TR(string,identifier,associator)
Parameters
string The target string. It can be a numeric value, a string literal, the name of a variable, or any valid Caché ObjectScript expression.
identifier The character(s) to search for in string. It can be a numeric value, a string literal, the name of a variable, or any valid Caché ObjectScript expression.
associator Optional — The replacement character(s) corresponding to each character in the identifier. It can be a numeric value, a string literal, the name of a variable, or any valid Caché ObjectScript expression.
Description
The $TRANSLATE function performs character-for-character replacement within a string. It processes the string parameter one character at a time. Initially, $TRANSLATE sets the output string to the input string. It compares each character in the input string with each character in the identifier parameter. If $TRANSLATE finds a match, it makes note of the position of that character.
$TRANSLATE is case-sensitive.
$TRANSLATE by itself does not change the string parameter. To change the input string, you must SET it equal to a translation of itself.
$TRANSLATE and $REPLACE
$TRANSLATE performs character-for-character matching and replacement. $REPLACE performs string-for-string matching and replacement. $REPLACE can replace a single specified substring of one or more characters with another substring, or remove multiple instances of a specified substring. $TRANSLATE can replace multiple specified characters with corresponding specified replacement characters.
By default, both functions are case-sensitive, start at the beginning of string, and replace all matching instances. $REPLACE has parameters that can be used to change these defaults.
Examples
In the following example, a two-parameter $TRANSLATE removes Numeric Group Separators based on the setting for the current locale:
AppropriateInput
   SET ds=##class(%SYS.NLS.Format).GetFormatItem("DecimalSeparator")
   IF ds="." {SET x="+1,462,543.33"}
   ELSE {SET x="+1.462.543,33"}
TranslateNum
   WRITE !,"before translation ",x
   SET ngs=##class(%SYS.NLS.Format).GetFormatItem("NumericGroupSeparator")
   IF ngs=","     {SET x=$TRANSLATE(x,",") }
   ELSEIF ngs="." {SET x=$TRANSLATE(x,".") }
   ELSEIF ngs=" " {SET x=$TRANSLATE(x," ") }
   ELSE {WRITE "Non-standard NumericGroupSeparator:", ngs
         RETURN }
   WRITE !,"after translation  ",x
 
In the following example, a three-parameter $TRANSLATE replaces various Date Separator Characters with slashes. Note that the associator must specify “/” as many times as the number of characters in identifier:
   SET x(1)="06-23-2014"
   SET x(2)="06.24.2014"
   SET x(3)="06/25/2014"
   SET x(4)="06|26|2014"
   SET x(5)="06 27 2014"
   FOR i=1:1:5{
        SET x(i)=$TRANSLATE(x(i),"- .|","////")
        WRITE "x(",i,") :",x(i),!
   }
 
In the following example, a three-parameter $TRANSLATE “simplifies” Spanish to basic ASCII by replacing accented letters with non-accented letters and removing the question and exclamation sentence prefix punctuation:
  SET esp="¿Sabes lo que ocurrirá en el año 2016?"
  WRITE "Spanish:",!,esp,!
  SET iden=$CHAR(225)_$CHAR(233)_$CHAR(237)_$CHAR(241)_$CHAR(243)_$CHAR(250)_$CHAR(161)_$CHAR(191)
  SET asso="aeinou"
  WRITE "Identifier: ",iden,!
  WRITE "Associator: ",asso,!
  SET spanglish=$TRANSLATE(esp,iden,asso)
  WRITE "Spanglish:",!,spanglish
 
Needless to say, this is not a recommended conversion for use on actual Spanish text.
See Also