%XLATE

25 Views


The %XLATE built-in function in RPGLE is used to translate characters in a string, similar to the TRANSLATE function in SQL or strtr() in PHP.


โœ… %XLATE Syntax

%XLATE(from: to: inputString)
  • from: A string of characters to replace.

  • to: A string of replacement characters (must match the length of from).

  • inputString: The string you want to translate.

Each character in inputString that appears in from is replaced by the corresponding character in to.


๐Ÿ“Œ Simple Example

**FREE

Dcl-S original Char(20) Inz('HELLO WORLD');
Dcl-S result   Char(20);

result = %XLATE('HELLO': '12345': original);

Dsply result; // Output: '12334 W4R3D'

*INLR = *ON;
Return;

๐Ÿ” What happened?

  • 'H' → '1'

  • 'E' → '2'

  • 'L' → '3'

  • 'O' → '4'

Characters not in the from string are left unchanged.


๐Ÿงพ Use Case Example: Uppercase to Lowercase

Dcl-S txt  Char(20) Inz('HELLO WORLD');
Dcl-S lower Char(20);

lower = %XLATE('ABCDEFGHIJKLMNOPQRSTUVWXYZ':
               'abcdefghijklmnopqrstuvwxyz':
               txt);
Dsply lower; // Output: 'hello world'

โ— Important Notes

  • Both from and to must be the same length, or you get a compile error.

  • Only single-character substitutions are supported — not full-word replaces.

  • %XLATE is case-sensitive.


 

Post Comments