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

How to pad spaces at the end of a string

Former Member
0 Kudos
14,272

Hi All,

I am new to ABAP and need help to achieve this functionality

I have to build a string with fixed lenth . my input string

is always lesser than equal to X . I need to calculate and

add that many spaces at the end of the input string .

I tried to do this and getting the following error

please help on this :

if strlen( t_resultc ) < 525.

RFILL(t_resultc,' ',525)

endif.

Error : Comma without preceding colon(after RFILL(T_RESULTC ?)

Q: Can I use RFILL with out Select statement in a ABAP program

Thanks & Regards

KLK

1 ACCEPTED SOLUTION
Read only

Former Member
5,780

Hi KLK,

you can not use the rfill statement, but this coding seems to work:


DATA:
   t_resultc TYPE string,
   spaces TYPE i.

t_resultc = 'test'.
spaces = 525 - STRLEN( t_resultc ).

DO spaces TIMES.
  CONCATENATE t_resultc space INTO t_resultc
  RESPECTING BLANKS.
ENDDO.

If t_resultc has a fixed length of 525 characters you could also define it like this:


data t_resultc(525) type c.

It would then automatically contain 525 spaces as default value.

Regards

Thomas Langen

12 REPLIES 12
Read only

former_member156446
Active Contributor
0 Kudos
5,780

if t_resultc is a character field it ill be carrying spaces by default.

if its not character type just say

t_resultc = space.

Read only

Former Member
5,781

Hi KLK,

you can not use the rfill statement, but this coding seems to work:


DATA:
   t_resultc TYPE string,
   spaces TYPE i.

t_resultc = 'test'.
spaces = 525 - STRLEN( t_resultc ).

DO spaces TIMES.
  CONCATENATE t_resultc space INTO t_resultc
  RESPECTING BLANKS.
ENDDO.

If t_resultc has a fixed length of 525 characters you could also define it like this:


data t_resultc(525) type c.

It would then automatically contain 525 spaces as default value.

Regards

Thomas Langen

Read only

0 Kudos
5,780

Hi Thomas,

Thanks for the quick responce .

t_resultc is of the type String not Char of fixed length.

I have one question with above code it would add integer

values to the string upto the length of 525

But I need spaces upto the fixed length so can I add character to string ?

I will test and get back.

Thanks & Regards

KLK

Read only

0 Kudos
5,780

Hi KLK,

the coding above adds space characters, not integers. It think you possibly have confused the data element spaces I defined in the coding with the SPACE character which I used in the concatenate statement.

Regards

Thomas

Read only

0 Kudos
5,780

Hi Thomas ,

Yes , I was confussed .

So I have added ' ' instead of space .

Thanks a lot for the Do usage syntax .

Please suggest any links or documentations to help me

learn these basic syntax in ABAP .

Thanks once again ,

KLK

Read only

0 Kudos
5,780

Hi,

I used the book "ABAP Objects" written by Keller and Krüger to learn the basics. The online help is quite useful, too.

Regards,

Thomas Langen

Read only

0 Kudos
5,780

Hi mate,

      Don't we have a simple function for this than manipulating characters.

Read only

0 Kudos
5,780

Hi,

YOU CAN WRITE YOUR LOGIC LIKE THIS:

if strlen( t_resultc ) < 525.

DO.

CONACTENATE T_RESULTC SPACE INTO T_RESULTC.

IF STRLEN(T_RESULTC) = 525.

EXIT.

ENDIF.

ENDDO.

ENDIF.

Read only

andrewdiaz2000
Active Participant
0 Kudos
5,780

Hi,

Try the following code but kindly make a note..

Note:-USE quote like this  ` `, Instead of normal single quotes ‘ ’

WHILE strlen( T_RESULTC ) < 525.            “Check the length   

   CONCATENATE  T_RESULTC ` ` INTO T_RESULTC. ”Add spaces at the end   

ENDWHILE.

Hope this helps,

Andrew

Read only

Former Member
5,780

With string templates you could do something like this:


t_resultc = |{ t_resultc WIDTH = 525 PAD = space }|.

Read only

ldzierza
Participant
5,780

see The ABAPVARCHARMODE: Blanks and Empty Strings in ABAP and SQLScript - Brandeis Consulting

data: lv_kunnr_tmp type string.

      DATA(cnt_kunnr) = strlen( kna1-kunnr ).

      IF NOT cnt_kunnr = 10.
          lv_kunnr_tmp = |{ kna1-kunnr WIDTH = 10 PAD = space }|.
      ELSE.
        lv_kunnr_tmp = kna1-kunnr.
      ENDIF.
Read only

0 Kudos
3,409

Thanks...