Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Padding character fields with zeros

Former Member
0 Likes
2,778

Hello,

I need to add zeros to the end of character fields TYPE C. I know i can use the conversion routines but that only works for numeric fields.

Thanks

1 ACCEPTED SOLUTION
Read only

former_member209703
Active Contributor
0 Likes
2,456

You can use OVERLAY

For instance, If your variable has 10 positions and you want to add zeros or whatever character at the end of it, you can use:


DATA: VAR TYPE C LENGTH 10.
VAR = 'TEXT'.
OVERLAY VAR WITH '0000000000'.

14 REPLIES 14
Read only

former_member209703
Active Contributor
0 Likes
2,457

You can use OVERLAY

For instance, If your variable has 10 positions and you want to add zeros or whatever character at the end of it, you can use:


DATA: VAR TYPE C LENGTH 10.
VAR = 'TEXT'.
OVERLAY VAR WITH '0000000000'.

Read only

0 Likes
2,456

Hello,

Thank you for the prompt reply.

I know of the OVERLAY solution but that only works if you have a continuos string in your field 'ie: no spaces between chars'.

If i have a field with "Thank you" in it and use the overlay trick then i end up with "Thank0you" which is not desirable. I only want to add zeros at the end of the field not replace all spaces with zeros.

Is there a way to achieve this?

Thanks

Read only

0 Likes
2,456

Hi,

You can use concatenate 0000 as string to your variable at end.

br,

Rahul

Read only

0 Likes
2,456

Hi,

then OVERLAY will still work... just add an offset:


  DATA: var TYPE c LENGTH 10,
        len TYPE i.

  var = 'THANK YOU'.
  len = STRLEN( var ).
  OVERLAY var+len WITH '0000000000'.

Kr,

m.

Read only

0 Likes
2,456

I can think of two ways.

Using a WHILE loop.

while strlen( ) < 10.

concatenate var '0' into var.

endwhile.

or get the length and write one field over the other.

data:

lf_len type i,

var_temp like var.

lf_len = strlen( var ).

var_temp = '0000000000'.

var_temp+0(lf_len) = var.

var = var_temp.

Read only

0 Likes
2,456

Thanks for all the replies.

STRLEN will give me the lenght of the field but i need the offset of the last chararcter.

Your post gave me an idea though. I'm doing the following and it's working.

value = 'Thank You '.

tmp = '00000000000000'.

FIND REGEX `\w[ [:space:] ][ [:space:] ]` IN value MATCH OFFSET moff.

IF SY-SUBRC = 0.

OVERLAY value+moff WITH tmp.

ENDIF.

The find looks for the last alphanumeric char and 2 spaces after it meaning the last alphanumeric char in the string.

Thanks to all, i will reward as fairly as possible.

Read only

0 Likes
2,456

>

> The find looks for the last alphanumeric char and 2 spaces after it meaning the last alphanumeric char in the string.

>

What happens if there is only 1 space at the end?

Read only

0 Likes
2,456

Hi,

Here is the solution

data ch(10).

data len type i.

data diff type i.

ch = 'text'.

len = strlen( ch ).

diff = 10 - len.

do diff times.

concatenate ch '0' into ch.

enddo.

write:/ ch. output : text000000

regards

Read only

0 Likes
2,456

Maen,

Very true, i have not considered it it and it is indeed very possible for it to happen.

Not sure yet what regex would apply to only the end of field and not take the middle spaces in between... If you know how then please let me know.

Thanks

Edited by: MonkD on Oct 5, 2011 4:00 PM

Read only

0 Likes
2,456

I had to add another FIND statement to take into account the scenario that was pointed out by Maen "Thank for that!!!".

value = 'Thank You '.

tmp = '00000000000000'.

FIND REGEX `\w[ :space:] [ :space:]` IN value MATCH OFFSET moff.

IF SY-SUBRC = 0.

OVERLAY value+moff WITH tmp.

ELSE.

FIND REGEX `\w[ :space:]\Z` IN value MATCH OFFSET moff.

IF SY-SUBRC = 0.

OVERLAY value+moff WITH tmp.

ENDIF.

ENDIF.

The second find will catch the scenario where there is one blank space before the end of the string.

Thanks to all.

Edited by: MonkD on Oct 5, 2011 4:37 PM

Read only

0 Likes
2,456

Hi,

STRLEN will give me the lenght of the field but i need the offset of the last chararcter.

Actually the length of the field value is the offset of last character

Just try the overlay as i proposed and check by yourself...

Kr,

m.

Read only

0 Likes
2,456

Indeed it is and i did test but i had the same number of chars in the field as the size of the var itself and that just trew me off....

i can accomplish my goal with the following as well. It takes less lines this way too.

var = 'hello '.

tmp = '0000000000'.

len = STRLEN ( var ).

describe FIELD var LENGTH olen IN CHARACTER MODE.

if len <> olen.

OVERLAY var+len WITH tmp.

ENDIF.

Thanks.

Read only

surajarafath
Contributor
0 Likes
2,456

Try Concatenate..

DATA var1(10) type c.
var1 = 'Thank You'.
CONCATENATE var1 '0000' into var1.
WRITE: var1.

Read only

former_member192432
Participant
0 Likes
2,456

Hi ,

Try below code it may help you.

DATA: VAR TYPE C LENGTH 10 ,

v_len type i ,

v_len1 type i.

VAR = 'TE XT'.

v_len = strlen( var ) .

v_len1 = 10 - v_len .

do v_len1 TIMES.

CONCATENATE var '0' INTO var .

enddo.

WRITE : var.

Regards

Chetan