‎2011 Oct 05 12:59 PM
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
‎2011 Oct 05 1:08 PM
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'.
‎2011 Oct 05 1:08 PM
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'.
‎2011 Oct 05 1:17 PM
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
‎2011 Oct 05 1:20 PM
Hi,
You can use concatenate 0000 as string to your variable at end.
br,
Rahul
‎2011 Oct 05 1:24 PM
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.
‎2011 Oct 05 1:24 PM
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.
‎2011 Oct 05 2:18 PM
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.
‎2011 Oct 05 2:21 PM
>
> 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?
‎2011 Oct 05 2:36 PM
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
‎2011 Oct 05 3:00 PM
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
‎2011 Oct 05 3:37 PM
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
‎2011 Oct 05 3:41 PM
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.
‎2011 Oct 05 4:08 PM
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.
‎2011 Oct 05 1:25 PM
Try Concatenate..
DATA var1(10) type c.
var1 = 'Thank You'.
CONCATENATE var1 '0000' into var1.
WRITE: var1.
‎2011 Oct 05 2:24 PM
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