Application Development 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: 

Appending Blanks to the right of a string.

Former Member
0 Kudos

Dear Friends

I want to append blanks to the right of a character string. However, I am not able to achieve it.

Are there any readymade Function modules provided by SAP to do the same or can you provide me some sample code to achieve the same.

I have written a dummy code to test it. But it does not work. Can I get some help on this ?

DATA: len TYPE i,

shift_len TYPE i,

new_len TYPE i,

test_str(10) type c value 'SUNDAR'.

START-OF-SELECTION.

len = strlen( test_str ).

IF len < 10.

shift_len = 10 - len.

SHIFT test_str BY shift_len places RIGHT.

SHIFT test_str by shift_len places CIRCULAR.

ENDIF.

new_len = strlen( test_str ).

WRITE: len,' ',new_len.

Wondering why the above program does not work.

Thanks in advance for your inputs.

Regards

Sundara

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Raghavan,

Why don’t you try out the following?

DATA: LEN TYPE I,
      SHIFT_LEN TYPE I,
      NEW_LEN TYPE I,
      TEST_STR TYPE STRING VALUE 'SUNDAR'.

START-OF-SELECTION.

  LEN = STRLEN( TEST_STR ).

  IF LEN < 10.
    SHIFT_LEN = 10 - LEN.
    SHIFT TEST_STR BY SHIFT_LEN PLACES RIGHT.
    SHIFT TEST_STR BY SHIFT_LEN PLACES CIRCULAR.
  ENDIF.

  NEW_LEN  = STRLEN( TEST_STR ).
  WRITE: LEN, '           ', NEW_LEN.

If you cannot use the STRING data type for some reason, please let me know why.

Regards,

Anand Mandalika.

7 REPLIES 7

Former Member
0 Kudos

I dont think this is possible.

But why do u want to do this ?

Former Member
0 Kudos

SAP will never store blanks at the end of a string.

a few examples for this.

1. concatenate 'SAP ' 'Test' into <str>.

the result will be SAPTest without blanks.

2. See the o/p of this program. It will re-iterate this in another way.

data:

str1(20) type c,

len type i.

str1 = 'abc .'. [give as many spaces such that the '.' is on the 20th character].

len = strlen( str1 ).

write:/ str1.

write:/ len.

shift str1 right by 1 places.

len = strlen( str1 ).

write:/ str1.

write:/ len.

Message was edited by: Prashanth Prabhu

Former Member
0 Kudos

Hi, Sundara!

Your test code works exactly as it should. Built-in STRLEN function returns length of a string (type STRING or XSTRING) or a character field (particularly type C) up to its last non-space character (its occupied length).

If for any reason you need STRLEN( test_str ) to return 10 and it is acceptable for you to add blanks to the left of a character string instead of to the right, you can use construction:

WRITE test_str TO test_str RIGHT-JUSTIFIED.

Regards,

Maxim.

Former Member
0 Kudos

Hi Raghavan,

Why don’t you try out the following?

DATA: LEN TYPE I,
      SHIFT_LEN TYPE I,
      NEW_LEN TYPE I,
      TEST_STR TYPE STRING VALUE 'SUNDAR'.

START-OF-SELECTION.

  LEN = STRLEN( TEST_STR ).

  IF LEN < 10.
    SHIFT_LEN = 10 - LEN.
    SHIFT TEST_STR BY SHIFT_LEN PLACES RIGHT.
    SHIFT TEST_STR BY SHIFT_LEN PLACES CIRCULAR.
  ENDIF.

  NEW_LEN  = STRLEN( TEST_STR ).
  WRITE: LEN, '           ', NEW_LEN.

If you cannot use the STRING data type for some reason, please let me know why.

Regards,

Anand Mandalika.

Former Member
0 Kudos

DATA char1(30) TYPE c VALUE 'Greate'.

DATA char2(30) TYPE c.

DATA char3(30) TYPE c.

CONCATENATE char1 char2 INTO char3.

WRITE char3.

*what i under stand from your question. char2 has blank spaces in it *coz it is not initialized. so you can use conatenate function to do

*this.

Former Member
0 Kudos

Sundara,

Try this....

<b>write : /(10) 'SundaraSundara', 'end'.

write : /(10) 'Sundara', 'end'.</b>

output

<b>SundaraSun end

Sundara... end</b>

<i>there will be one space in between because of the write (... = 3 spaces)</i>

This will ensure that always 10 (for example) characters are written.

Rishi

former_member183804
Active Contributor
0 Kudos

Hello Sundara,

the problem is char fields (type c) aren´t strings. They are sequences of chars with a fixed buffer sized and blank as terminator. Therefore you can never have a blank as trailing field of char field.

With the newer releases there is also a true string type.


  data:
    char_sequence  type c length 10,  "no trailing spaces"
    char_string    type string.       "can have trail. blank"

This new string type can also contain trailing spaces.

Best Regards

Klaus