Create a useful procedure to remove white space from strings in RPG.
In a previous article, I discussed how to use string manipulation built-in functions (BIFs) to process external files. I explained %TRIM, %TRIML, %TRIMR, %SUBST, %XLATE, %CHECK, and %CHECKR. But it was brought to my attention that I failed to mention the crucial string manipulation %SCAN BIF when an MC Press Online reader named Donna G. asked me, "Do you have any suggestions on how to strip out the blanks that are in the middle of data?"
She wants to take information that is coming from an EDI 850 PO document as this…
PID*F****BOX – GENIE 20 X 14 X 14µ
…and convert it to an XML document to look like this:
<Comments>BOX - GENIE 20 X 14 X 14</Comments>
We can remove the junk characters by implementing the BIFs mentioned in the previous article. And creating the XML was not an issue for Donna. So let's focus on removing the spaces by creating a reusable procedure to implement the %SCAN BIF:
%SCAN(search argument : source string { : start})--
This function returns an unsigned integer that indicates the first position where the search argument is found within the source string. If the search argument is not found in the source string, then the returning value will be zero. The start parameter is optional and indicates the position in the source string where the scanning will begin. If the start parameter is not specified, it will default to start the scan at the beginning of the source string.
The squeezeString Procedure
To easily support the need to remove spaces from the middle of the data, we will create a procedure that uses the %SCAN BIF to find the spaces within the string.
When a space has been found, we will do two things:
- Split the string at the point that the space was found using a combination of the %TRIM and %SUBST BIFs to trim both the front and back of the string to remove the surrounding spaces.
- Drop a single space back into the string to ensure that there is only one space.
Then it's just a matter of lather, rinse, and repeat until we get to the end of the string.
Once the end of the string has been reached, we add the remaining characters from the final space search, and we're done.
*-----------------------------------------------------------------
* squeezeString - Squeezes out multiple spaces
*-----------------------------------------------------------------
P squeezeString...
P B EXPORT
D squeezeString...
D PI 65535A varying
D argInBytes 65535A const varying
D posi S 5S 0
D inBytes S 65535A varying
D outBytes S 65535A varying
D********************************************************************
/free
inBytes = %trim(argInBytes);
outBytes = *BLANKS;
posi = %scan(' ':inBytes);
dow posi > 0;
outBytes = %trim(outBytes) + ' '
+ %subst(inBytes:1:posi);
inBytes = %trim(%subst(inBytes:posi));
if (inBytes = *BLANKS);
leave;
else;
endif;
posi = %scan(' ':inBytes);
enddo;
if (inBytes = *BLANKS);
else;
outBytes = %trim(outBytes) + ' '
+ %trim(inBytes);
endif;
return %trim(outBytes);
/end-free
P E
If you would like to remove all of the spaces, modify the procedure to not put the single space back into the outBytes when it is being built.
Using the squeezeString Procedure
I am not familiar with EDI 850 PO documents, so I am unsure of the patterns to identify junk. I will just trim off the first nine characters using the %SUBST BIF for this example. But, if there are patterns, you could easily implement additional logic to handle that using a combination of the string manipulation BIFS from this article and from the previous article.
D inputBytes S 100A
D outputBytes S 100A
D displayBytes S 52A
D squeezeString...
D PR 65535A varying
D argInString 65535A const varying
C*
/free
inputBytes = 'PID*F****BOX - GENIE'
+ ' 20 X 14 X 14µ';
outputBytes = %subst(inputBytes:10);
outputBytes = squeezeString(outputBytes);
displayBytes = 'squeezeString('
+ %trim(outputBytes)
+ ')';
DSPLY displayBytes;
*inlr = *ON;
/end-free
The Output
After running the program, you will see this output:
DSPLY squeezeString(BOX - GENIE 20 X 14 X 14µ)
When the strings are displayed, they are surrounded by parentheses to identify any leading and trailing blanks.
Download the Code
You can download the code used in this article--as well as the fixed-format version--by clicking here.
Thanks for the great question, Donna! I hope the code works well for you and everyone else who needs these capabilities.



Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:
LATEST COMMENTS
MC Press Online