|
Hi Booth,
How is the easiest/best way to center a string in a field? Say the string is "123 test string 321" and I want it centered in a 65 character field that may or may not have a value there already. 1) the new value will be just the string, centered. 2) the new value will overlay whatever happens to be in the string.
Here's the routine that I use for such things. It's intended to be generic enough that it'll work for any string that's 256 chars or shorter:
**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** util_center(): Center text in a field
**
** peText = text to center
** peSize = size of output field
**
** Returns the centered text.
**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P util_center B export
D util_center PI 256A
D peText 256A varying const options(*varsize)
D peSize 10I 0 value
D wwPos s 10I 0
D wwRet s 256A
/free
wwRet = *blanks;
wwPos = ((peSize/2) - (%len(peText)/2)) + 1;
%subst(wwRet:wwPos:%len(peText)) = peText;
return wwRet;
/end-free
P E
To call it (using your example) you'd do the following:
D myString s 65A
/free
myString = '123 test string 321';
myString = util_center(myString: %size(myString));
/end-free
I keep this routine in a service program that's in a binding directory
that I specify with every compile that I do, so that anytime I want to
center a string, I just code "util_center", as if it's a part of the RPG
language. :)
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.