Find Length of a String in ABAP using STRLEN function
To find the length of a string variable or character data in ABAP, developers use ABAP STRLEN string function. STRLEN ABAP string length function is also used to validate input string before using it or assigning to an other variable.
Below ABAP string code calculates the length of the input string variable using ABAP string function STRLEN, stores the length in an integer variable. Then using WRITE function, the length of the input string variable is displayed on the screen.
data lv_str type string.
data lv_int type i.
data lv_length type string.
lv_str = 'Kodyaz'.
lv_int = STRLEN( lv_str ).
lv_length = lv_int.
CONCATENATE lv_str 'has a length of' lv_length 'characters' INTO lv_str separated by space.
CONDENSE lv_str.
write lv_str.
An other sample ABAP code below is validating the length of a date variable. If the input string variable has a length of 8 characters then it converts the date string into another date format.
data LV_STR_DATE type CHAR10.
if STRLEN( LV_STR_DATE ) = 8.
concatenate LV_STR_DATE+4(4) LV_STR_DATE+2(2) LV_STR_DATE(2) into LV_STR_DATE.
write LV_STR_DATE.
endif.