Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
rainer_hbenthal
Active Contributor

Character variables

Variables of type c  have a specified length declared in the TYPES or DATA declaration section. The length can not change, and there is no length indicator nor an end-of-char like \0 in c. Blanks to the right of the content are always ignored by the system, a char variable containing only blanks is treated as empty/initial. Clearing a char variable will fill it completly with blanks.

DATA: cvar    TYPE c  LENGTH 20,
         len      TYPE i.

cvar = ' '.
len = STRLEN( cvar ).
WRITE:/ 'Charlen: ', len. 

A blank is assigned to the variables, but still the length is zero. The compiler cant differ between internally used blanks and blanks assigned by the programmer.

There is one exception to this rule: the separated by clause in the concatenate command. Here a ' ' will be a blank and not an empty character sequence.-1 characters, but this is rather a theoretical value, more often you will be limited by the profil parameter ztta/max_memreq_MB or by the amount of concurrent running processes.

As strings do have a length indicator they can contain blanks even at the end of the content:

DATA: svar    TYPE string,
         len      TYPE i.

svar = ' '.
len = STRLEN( svar ).
WRITE:/ 'Stringlen: ', len. 

If you run this little piece of code, the result will be .... zero! But don't have strings the ability of storing blanks? Shouldn't the result be one? The trap we felt into was using a character literal: ' '. A character literal has the same restrictions as character variables: blanks at the end are ignored, an initial value is treated as empty. So  at least we assigned an empty character value to a string which will be of course empty, too.

Using a string literal will avoid this. The difference between a character literal and a string literal are the enclosing tags: apostrophes for a character literal and back quotes for string literals. Using a string literal we will get the expected result:

DATA: svar    TYPE string,
         len      TYPE i.

svar = ` `.
len = STRLEN( svar ).
WRITE:/ 'Stringlen: ', len. 

Note: the keyword SPACE is a predefined character literal with length 1. Consequently, assigning SPACE to a string variable will not assign a blank! Using string literals will help us in cases where we really need a blank. Imagine you want to replace all semicolons in a variable with blanks:

  cvar = 'A;B;C;D;E'.
  REPLACE ALL OCCURRENCES OF ';' IN cvar WITH ' '.          " (1)
  WRITE:/ cvar.

  REPLACE ALL OCCURRENCES OF ';' IN cvar WITH space.        " (2)
  WRITE:/ cvar.

  REPLACE ALL OCCURRENCES OF ';' IN cvar WITH ` ` .         " (3)
  WRITE:/ cvar.

  svar = `A;B;C;D;E`.
  REPLACE ALL OCCURRENCES OF ';' IN svar WITH ` `.          " (4)
  WRITE:/ svar.

Now we can understand why (1) and (2) will not give the desired result, the character literal is initial/empty so the result will be ABCDE without any blank in between. (3) will not work either, the string literal will be converted to character cause the target is a character variable. Only (4) will give us the result A B C D E.

!! Unfortunately, i had an error in my test report, so (3) will work giving the desired result A B C D E !!

h3. Mixing with other types

Be careful when mixing character and string variables with other data types:

DATA:

cvar TYPE c LENGTH 20,
svar TYPE string,
pvar TYPE p LENGTH 5,
l TYPE i.

pvar = 5.

cvar = pvar.
l = STRLEN( cvar ).
WRITE:/ 'Content: |', cvar, '| (', l, ')'.

svar = pvar.
l = STRLEN( svar ).
WRITE:/ 'Content: |', svar, '| (', l, ')'.

 

Using the character variable the 5 is right justified with a blank at its end. The blank at the end is the sign, and as trailing blanks are not part of the content the length is 19.

Using the string variable the 5 is left justified with a trailing blank which is the sign. As blanks at the end in strings are part of the content the length is 2.

So be careful when assigning types to chars or strings, check if the result fullfill your needs.

h3. Disadvantages using strings

Performance

Performance is not really an issue, even when assigning character literals to strings the needed type conversion is beneath notice.

h4. Substrings

Using character variables you can assign new values to a substring:

cvar = 'Hello World'.
cvar+5(1) = ','.
write:/ cvar.

When you try this with a string you will get a compiler error saying that you cannot use offest und length specification. Instead, you need to use the REPLACE command:

svar = 'Hello World'.
" svar+5(1) = ','. <= compiler error
REPLACE SECTION OFFSET 5 LENGTH 1 OF svar WITH ','.
WRITE:/ svar.

So, you have to write a little bit more code.

h4. Translations

If you need to translate your report to different languages you can just add a nummer in brackets after the literal +'Error Message'(001) +which is more readable as text-001. This is not working with string literals.

h3. Helping classes

Check  CL_ABAP_CHAR_UTILITIES and CL_ABAP_STRING_UTILITIES to find some useful methods.</p>

10 Comments