ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for Character String Processing →  Character String Functions (cstring_func) →  cstring_func - Description Functions → 
Mail Feedback

cstring_func - charlen, dbmaxlen, numofchar, strlen

These built-in length functions have an unnamed character-like argument.


... func( arg ) ...


The following table shows the length functions with an unnamed argument. The arguments arg of all length functions except dbmaxlen are character-like expression positions. The argument of dbmaxlen is a character-like functional operand position. The return value has the type i for all length functions.

Function func Return Value
charlen Length of the first character of arg in the code page used: 1 for a single Unicode character; 2 for surrogate pairs.
dbmaxlen Maximum length of a string defined in the ABAP Dictionary (RAWSTRING, SSTRING, STRING, or GEOM_EWKB). If the string is unrestricted, the constant abap_max_db_string_ln or abap_max_db_rawstring_ln from the type pool ABAP is returned. The latter is also returned for the built-in ABAP types string and xstring.
numofchar Number of characters in arg, where trailing blanks are neither counted in data objects with fixed lengths nor in data objects with the type string.
strlen Number of characters in arg, where trailing blanks in data objects with fixed lengths are not counted, whereas in data objects with the type string they are.


The results of the following length determinations are 10 and 5.

  str TYPE string      VALUE `12345     `,
  txt TYPE c LENGTH 10 VALUE '12345     '.

cl_demo_output=>display( |{ strlen( str )
                       }, { strlen( txt ) }| ).


The result of function strlen is 2 because ABAP handles the surrogate pair in the character string as two UCS-2 characters. When counting with a regular expression in PCRE syntax that is introduced with (*UTF), the result is 1, because the surrogate pair is interpreted as one UTF-16 character.

FINAL(surrogate_pair) = cl_abap_codepage=>convert_from(
  codepage = 'UTF-8'
  source    = CONV xstring( 'F09F91BD' ) ).
                            "U+1F47D, EXTRATERRESTRIAL ALIEN

cl_demo_output=>write_text( surrogate_pair ).

FINAL(ucs2_len)  = strlen( surrogate_pair ).
cl_demo_output=>write( ucs2_len ).
FINAL(utf16_len) = count( val = surrogate_pair pcre = `(*UTF).` ).
cl_demo_output=>write( utf16_len ).

cl_demo_output=>display( ).