TRANSLATE

TRANSLATE returns a character string of the same length as x.

Read syntax diagramSkip visual syntax diagramTRANSLATE( x, y, z)
x
Character expression to be searched for possible translation of its characters.
y
Character expression containing the translation values of characters.
z
Character expression containing the characters that are to be translated. If z is omitted, it defaults to collate().

TRANSLATE operates on each character of x as follows:

If a character in x is found in z, the character in y that corresponds to that in z is copied to the result; otherwise, the character in x is copied directly to the result. If z contains duplicates, the leftmost occurrence is used.

y is padded with blanks, or truncated, on the right to match the length of z.

Any arithmetic or bit arguments are converted to character.

TRANSLATE supports UCHAR data. But if x has UCHAR type, then z must not be omitted.

TRANSLATE does not support GRAPHIC or WIDECHAR data.

TRANSLATE will perform best when the second and third arguments are either literals, named constants declared with the VALUE attribute, or restricted expressions.

Example

  dcl source char value("Ein Raetsel gibt es nicht.");
  dcl target char(length(source));
  dcl (to   value ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
       from value ('abcdefghijklmnopqrstuvwxyz')) char;

  target = translate(source, to, from);
           /* "EIN RAETSEL GIBT ES NICHT." */

Note that you could also use the UPPERCASE built-in for the same purpose as the TRANSLATE built-in in the example above. However, while the UPPERCASE built-in function will translate only the standard alphabetic characters, TRANSLATE can be used to translate other characters. For example, if "Raetsel" were spelled with an ä-umlaut, TRANSLATE could translate the ä-umlaut to Ä-umlaut if those characters were added to the from and to strings, respectively.