Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Strange: blank in string. Anybody can explain this?

Hello!

Look at this. Until now I thougt, I had understood ABAP...

Output of the following programm:

1) Strange

2) Strange

3) Strange

4) Strange

5) Strange

6) Strange

Very strange, isn't it?

ABAPDOCU tells, that strings pay attention to blanks.

Can anybody explain this?

REPORT zstrange.

DATA: stringwithspace TYPE string VALUE 'A B',
      teststring TYPE string,
      blankstring TYPE string VALUE ' ',
      l type i.

START-OF-SELECTION.
  IF stringwithspace+1(1) = ' '.
    WRITE: / '1) My guess'.
  ELSE.
    WRITE: / '1) Strange'.
  ENDIF.

  IF stringwithspace+1(1) = blankstring.
    WRITE: / '2) guess'.
  ELSE.
    WRITE: / '2) Strange'.
  ENDIF.

  IF stringwithspace+1(1) = ''.
    WRITE: / '3) guess'.
  ELSE.
    WRITE: / '3) Strange'.
  ENDIF.

  teststring = stringwithspace+1(1).
  IF teststring = ' '.
    WRITE: / '4) Guess'.
  ELSE.
    WRITE: / '4) Strange'.
  ENDIF.

  IF teststring = blankstring.
    WRITE: / '5) Guess'.
  ELSE.
    WRITE: / '5) Strange'.
  ENDIF.

  IF teststring = ''.
    WRITE: / '6) guess'.
  ELSE.
    WRITE: / '6) Strange'.
  ENDIF.

Former Member
Former Member replied

No, thats not strange.

c variables have always a fixed length, it can not contain "nothing". If there are only spaces in, the c variable is treated as empty/initial.

Strings can contain nothing, and they can contain spaces. A string containing blanks is not interpreted as empty/initial.

      blankstring TYPE string VALUE ' ',

What you are doing here is assigning an empty string to the string var blankstring, it does NOT contain a blank.

Constants encapsulated with apostrophes are treated as c variables. But: constants encapsulated within back quotes will be interpreted as string. Change your code to

      blankstring TYPE string VALUE ` `,

Take care of that i used back quotes now, not apostrophes, Now the var blankstring contains a blank and the result in that case will change to MY guess.

Similar the camparison

  IF stringwithspace+1(1) = ' '.

This is comparing a blank with an empty char and thats why you are getting strange.

Change this to

  IF stringwithspace+1(1) = ` `.

and you will get My guess cause now you are comparing a blank with a blank and not a blank with an empty string.

Hope its now clear.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question