1

In RPG IV how can I take a string and eliminate all instances of a character in specific or replace them with another one ?. This is kind of like string replace built in methods in other programmnig languages. Ex: take 021-123450-23-4 and covert to 021123450234

mike
  • 1,233
  • 1
  • 15
  • 36
lalala2007
  • 823
  • 1
  • 8
  • 13

5 Answers5

4

The correct syntax for %xlate is:

%XLATE(from:to:string{:startpos})

To replace all hyphens with ampersands:

new_string = %xlate('-':'&':string);

To remove all hyphens:

You cannot remove a character using &xlate. 7.1 gives us %scanrpl, but prior to that, use something like this:

for i = 1 to %len(string);
    char = %subst(string:i:1);
    if char <> '-';
        new_string += char;
    endif;
endfor;
Community
  • 1
  • 1
Paul Morgan
  • 31,226
  • 3
  • 24
  • 27
3

Take a look at the following articles:

These should help.

Mike Wills
  • 20,959
  • 28
  • 93
  • 149
2

i had the same problem. so i wrote my own RPG procedure that does it for me:

     **
     **
     D************************************************************************
     D*                                                                      *
     D*  Procedure 'skReplace' -- Replaces text in 'text' string,            *
     D*                           searching for 'find' string,               *
     D*                           replacing with 'new' string.               *
     D*                           All occurances are replaced, not just one. *
     D*  Parameters: @txt = 'text' string                                    *
     D*              @fnd = 'find' string                                    *
     D*              @new = 'new' string (that replaces 'find' in 'source')  *
     D*                                                                      *
     D*  Update history:                                                     *
     D*  2013-04    Created by Shawn Kovac.                                  *
     D*                                                                      *
     D************************************************************************
     D*
     P skReplace       B
     D skReplace       PI           999A   Varying
     D    @txt                      999A   VALUE Varying
     D    @fnd                      999A   VALUE Varying
     D    @new                      999A   VALUE Varying
     D    @pos         S              3  0
     D*
      /free
       if (%Len(@fnd) = 0); // text to find cannot be empty.
          return @txt;
       endif;
       @pos = 1;
       dou (@pos = 0);
           @pos = %scan(@fnd: @txt: @pos);
           if (@pos > 0);
               @txt = %replace( @new : @txt : @pos : %Len(@fnd) );
               @pos = @pos + %Len(@new);
               if (@pos > %Len(@txt));
                   @pos = 0;
               endif;
           endif;
       enddo;

       return @txt;

      /end-free
     P skReplace       E
     **
     **

Since RPG is very picky about which column things are in, when you copy and reuse this code, you might need to adjust your pasted text, so there are 5 spaces before 'D*', '**', and 'P skReplace ...'. Six spaces are before '/free'. And all code between the '/free' lines have 7 or more spaces.

I welcome any suggestions for improvement of this code. I also have procedures for Left, Right, and Mid if anyone wants them. Just msg me if you do. I'm happy to share them. I know RPG has a '%subst' function, but many programming languages are picky like they'll error if parameters are invalid. Mine instead give more flexibility, for instance, Left('aoeu', -1) returns 'aoe' (1 character short of the full string), and Right('aoeu', -1) returns 'oeu' (the right part of the string after 1 character is removed). My Mid procedure also allows a negative start position, indexing from the end of the string too, which i found to be useful to me. But they're free for anyone who wants to spend the time to ask me for them.

Happy Coding!

Shawn Kovac
  • 1,425
  • 15
  • 17
0

To remove a character you can use this

strRes = %scanrpl('-':'':strSrc);
0

if your string always has that format ( 3 letters)-(6 letters)-(2 letters)-(1 letter) you can use a data structure: DS 15 Mystuc 1 3 part1 5 10 part2 12 13 part3 15 15 part4

then move each part of mystruct to another structure to get the full number or the string without the - movel Michar mystruc

this example is for rpg in as400 ( not rpgile)

mike
  • 1