On RPG-LE Development

Postings on RPG-LE and i5/OS

Archive for the ‘RPGLE Functions : %SUBST’ Category

Strip leading characters from a string

leave a comment »

In the never ending loop shown below, STRING is the variable containing the string value which needs leading characters, most commonly leading zeros, stripped from the value.

The following %scan operation shown below tests position 1 of the string to determine if the value found is a ’0′ (character zero).

           position = %scan ('0':STRING:1);

A test for ’0′ is made on position 1 of the string. If found, the %subst operation is used to remove it.

The string is then trimmed and the loop continues for the next test of position 1.

The loop ends when there are no more leading characters to be stripped.

//Strip off any leading zeros from STRING
position = 0;
dou position < 0;

   position = %scan ('0':STRING:1);
   if (position = 1);
     %subst(STRING: position : 1) = ' ';
     STRING= %triml(STRING);
   ELSE;
     leave;
   ENDIF;

enddo;
Follow

Get every new post delivered to your Inbox.