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: 

Strange: blank in string. Anybody can explain this?

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

rainer_hbenthal
Active Contributor
0 Kudos

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.

4 REPLIES 4

Former Member
0 Kudos

in debug it shows following

Variable Value Hexadecimal Value

TESTSTRING 2000

STRINGWITHSPACE A B 410020004200

STRINGWITHSPACE+1(1) 2000

BLANKSTRING

-


if change data declaration as

blankstring TYPE xstring VALUE ' ',

code gives syntax err "You cannot assign an initial value to an Xstring, internal table, or reference . . "

somehow Blankstring looks INITIAL (no hexadecimal value) hence not eq to teststring.

need to find why, but thought let's share finding so far.

rainer_hbenthal
Active Contributor
0 Kudos

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.

matt
Active Contributor
0 Kudos

Try this version of your program:

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.

output:

1) My guess

2) guess

3) Strange

4) Guess

5) Guess

6) Strange

Note - a string is quoted using `` A character field is quoted using ''.

matt

ThomasZloch
Active Contributor
0 Kudos

<somewhat duplicate answer removed, the experts were quicker>

Edited by: Thomas Zloch on May 5, 2009 1:43 PM