rohan_rege
13th February 2003, 19:04
hi,

i need to know how to fill the unfilled spaces in a string at the left with zeroes.

i.e if the string is 10 char long and curr the value is
123 ,then i got to make it 0000000123 i.e right justify it and then append zeroes to the left so that total length is 10

also i need some help on filling the unfilled spaces in a string after left justifying it with blank spaces

i.e if string is 10 char long and curr value is

" roger" it shud get left justified and then total length shud be 10 after appending blank spaces to it .
"roger "


i havent worked much on string operations and any help will be appreciated...

rgds
rohan

popeye
13th February 2003, 19:17
Hi Rohan,
A function like the one shown below should work.
If you want you could make it more generalized.
This should get u started.
Hope this helps.
Cheers,
Popeye


function pad.string(ref domain tcmcs.str10 hold.string)
{
hold.string = string.set$("-", 10 - len(hold.string)) & hold.string
}

mgakhar
13th February 2003, 19:32
Rohan,
If u want to append spaces to a string after left justifying it, one way to do it would be to define the variable as fixed

domain tcdsca test fixed

MG.

RobertB
14th February 2003, 14:36
Hi Rohan,

Try this general string-padding function I just slapped together: | Declare some variables to pass to the function by reference...
string my.pad.chars(10), my.string(30)

| Call the function a couple of times...
my.pad.chars = "Ax"
my.string = "hallo world"
message("Pad Left: %s", string_pad("L", my.pad.chars, my.string, 20))
message("Pad Right: %s", string_pad("R", my.pad.chars, my.string, 20))
message("Pad Center: %s", string_pad("C", my.pad.chars, my.string, 20))



|******************************************************************************
function string string_pad( string i.pad.type(1), ref string i.pad.chars(),
ref string i.string(), long i.length.out )
{
long str.len, pad.len, pad.chars.len, ignore.val, half.len
string ret.string(255), pad.string(255)

| Strip white-space at both ends of input-string and pad-string...
i.string = shiftl$(strip$(i.string))
i.pad.chars = shiftl$(strip$(i.pad.chars))

| Calculate length of padding required...
str.len = len(i.string)
pad.chars.len = len(i.pad.chars)
pad.len = i.length.out - str.len

if (pad.len < 1) then
ret.string = i.string
else
ignore.val = set.mem(pad.string, i.pad.chars, pad.len)
on case toupper$(i.pad.type)
case "L": | Left-pad....
ret.string = pad.string(1; pad.len) & i.string
break
case "R": | Right-pad...
ret.string = i.string & pad.string(1; pad.len)
break
case "C": | Center-pad...
half.len = pad.len / 2
ret.string = pad.string(1; half.len + 1) & i.string & pad.string(1; half.len)
break
default:

break
endcase

endif

return(ret.string)
}
It's rough and ready, but it works :D
HTH