onlyforbopi
3/3/2017 - 1:50 PM

ABAP - STRING MANIPULATION

ABAP - STRING MANIPULATION

*********************************************************
* Substring
DATA: email(25) TYPE C VALUE 'john_stewart@university.com',
		domain(10) TYPE C.
		
domain = email+13(10).
WRITE: / domain.

*format : var = var+<digitsintostring>(length)

* ie

domain = email+13(9).           "universit"
WRITE: / domain.
domain = email+12(9).			"@universi"
WRITE: / domain.
********************************************************************
* Calculate string length
length1 = STRLEN( str1 ) .
WRITE: / 'Length of str1', length1.
length1 = STRLEN( str2 ) .
WRITE: / 'Length of str1', length1.
length1 = STRLEN( str3 ) .
WRITE: / 'Length of str1', length1.
length1 = STRLEN( str4 ) .
WRITE: / 'Length of str1', length1.
length1 = STRLEN( str5 ) .
WRITE: / 'Length of str1', length1.
*******************************************************
* SPLIT STATEMENT
DATA:  ip1(15) TYPE C,
       ip2(15) TYPE C,
       ip3(15) TYPE C,
       split_string(15) TYPE C VALUE '90-90-90-90'.

SPLIT split_string at '-' INTO ip1 ip2 ip3.

WRITE: / ip1,
       / ip2,
       / ip3.
********************************************************
* SHIFT STATEMENT 
DATA: first_name(11) TYPE C VALUE '123JAMES456'.
WRITE: / first_name.
SHIFT first_name BY 3 PLACES LEFT.
WRITE: / first_name.
SHIFT first_name BY 3 PLACES RIGHT.
WRITE: / first_name.
*********************************************************************
* REPLACE STRING
DATA: stri1(20) TYPE C VALUE 'Panagos',
      stri2(20) TYPE C VALUE 'Den',
      stri3(20) TYPE C VALUE '192,193',
      stri4(20) TYPE C VALUE '100,101,102,103'.

WRITE: / stri1,
       / stri2,
       / stri3.

* Replace one char, first occurrence
REPLACE ',' WITH ':' INTO stri3.

WRITE: / stri1,
       / stri2,
       / stri3.

* Replace one string, first occurrence
REPLACE 'en' WITH 'ALALA' INTO stri2.

WRITE: / stri1,
       / stri2,
       / stri3.

* Replace multiple occurrences of string. 
REPLACE ALL OCCURRENCES OF ',' IN:
    stri3 with '##',
    stri4 with ':o:'.

WRITE: / stri1,
       / stri2,
       / stri3,
       / stri4.
*********************************************************
* OVERLAY TEXT 
DATA: csv_text(25) TYPE C VALUE 'John and Paul',
      comma_text(25) TYPE C VALUE '    ,   ,    .',
      var5(25) TYPE C.

WRITE: / csv_text,
       / comma_text.

OVERLAY csv_text WITH comma_text.
WRITE sy-subrc.

WRITE: / csv_text.
*********************************************************************
* CONDENSE STRING
CONDENSE str5.
length2 = STRLEN( str5 ).
WRITE: /'Condensed string: ', str5, / 'Length: ', length2.
CONDENSE str5 NO-GAPS.
length2 = STRLEN( str5 ).
WRITE: /'Condensed string: ', str5, / 'Length: ', length2.
* CONCATENATE STRINGS
DATA: str1(20) TYPE C VALUE 'Hello',
      str2(20) TYPE C VALUE 'World',
      str3(20) TYPE C,
      str4 TYPE C,
      str5(30) TYPE C,
      length1 TYPE I,
      length2 TYPE I,
      length3 TYPE I,
      cond1 TYPE C,
      cond2 TYPE C.

WRITE: / 'String 1: ', str1,
       / 'String 2: ', str2.

* Concatenate into two values
CONCATENATE str1 str2 INTO str3.
WRITE: / 'String3: ', str3.

* Concatenated into variable with no defined size
CONCATENATE str1 str2 INTO str4.
WRITE: / 'String4: ', str4.

* Concatenated with defined separator
CONCATENATE str1 str2 INTO str5 SEPARATED BY ' '.
WRITE: / 'String5: ', str5.

* Concatenated with defined separator and more than one parameter.
CONCATENATE str1 str2 str3 INTO str5 SEPARATED BY ' '.
WRITE: / 'String6: ', str5.
1. abapconcat.abap          : Concatenate strings
2. abapstringlength.abap    : Calculate string length
3. abapcondense.abap        : Condense string (remove spaces etc)
4. abapreplace.abap         : Find/Replace in string
5. abapsplit.abap           : Split string / Parse string from delimiter
6. abapshift.abap           : Shift string left or right
7. abapoverlay.abap         : Overlay a string with another.
8. abapsubstring.abap       : Substring a string