| Use RPG String Manipulation Built-In Functions to Remove Spaces |
|
|
|
| Programming - RPG | |||||
| Written by Thomas Snyder | |||||
| Monday, 31 August 2009 09:49 | |||||
|
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…
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 ProcedureTo 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:
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 ProcedureI 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 OutputAfter 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 CodeYou 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. | |||||
View all articles by this author
|
|||||
| Last Updated on Wednesday, 02 September 2009 07:21 |








I used to use %replace to remove the whitespace from a string (code below), but I am going to go back and tweak the service program to use %subst to increase its efficiency.
d whitespace pr a varying len(4193271) d text a varying const len(4193271) p whitespace b export d whitespace pi a varying len(4193271) d textin a varying const len(4193271) d textout s a varying len(4193271) d done s n inz(*off) /free textout = %triml(textin); dou done; monitor; textout = %replace(:textout:%scan(:%trimr(textout)):2); on-error *all; done = *on; endmon; enddo; return textout; /end-free p whitespace eswitching to %scan and %trim like in the article made the procedure over 3 times fasterd whitespace pr a varying len(4193271) d text a varying const len(4193271) p whitespace b export d whitespace pi a varying len(4193271) d textin a varying const len(4193271) d textout s a varying len(4193271) inz() d done s n inz(*off) d position s 10u 0 inz(0) /free textout = %triml(textin); dou done; position = %scan(:%trimr(textout): position + 1); if position = 0; leave; endif; textout = %subst(textout:1: position) + %triml(%subst(textout: position)); enddo; return textout; /end-free p whitespace e