SAP ABAP — String Operations
Strings are sequence of characters which used in ABAP programming largely. We use data type C variables for holding alphanumeric characters, with a minimum of 1 character and a maximum of 65,535 characters.
CONCATENATE
Combines two or more strings into one string.
Example :
REPORT Z_ABAP_DEVELOPER_TEST.
DATA: GV_S1 TYPE STRING VALUE ‘Welcome’,
GV_S2 TYPE STRING VALUE ‘to’,
GV_S3 TYPE STRING VALUE ‘ABAP’,
GV_S4 TYPE STRING VALUE ‘Tutorial’,
LT_RESULT3 TYPE STRING,
LT_RESULT1(40),
LT_RESULT2(40).
TYPES LT_RESULT3 TYPE C LENGTH 40.
DATA: LT_ITAB TYPE TABLE OF LT_RESULT3.
CONCATENATE GV_S1 GV_S2 GV_S3 GV_S4 INTO LT_RESULT1.
CONCATENATE GV_S1 GV_S2 GV_S3 GV_S4 INTO LT_RESULT2 SEPARATED BY ‘/’.
WRITE / LT_RESULT1.
SKIP 1.
WRITE / LT_RESULT2.
SKIP 1.
APPEND ‘Welcome’ TO LT_ITAB.
APPEND ‘to’ TO LT_ITAB.
APPEND ‘ABAP’ TO LT_ITAB.
APPEND ‘Tutorial’ TO LT_ITAB.
CONCATENATE LINES OF LT_ITAB INTO LT_RESULT3 SEPARATED BY space.
WRITE / LT_RESULT3.
Output :
SPLIT
Used to split the contents of a field into two or more fields.
Example :
REPORT Z_ABAP_DEVELOPER_TEST.
DATA: x1(10), y2(10), z3(10),
source(20) VALUE ‘abc-def-ghi’.
SPLIT source AT ‘-’ INTO x1 y2 z3.
WRITE:/ ‘X1 — ‘, x1.
WRITE:/ ‘Y2 — ‘, y2.
WRITE:/ ‘Z3 — ‘, z3.
Output :
SEARCH
To run searches in character strings.
Example :
REPORT Z_ABAP_DEVELOPER_TEST.
DATA: string(30) VALUE ‘SAP ABAP Developers’,
str(10) VALUE ‘ABAP’.
SEARCH string FOR str.
IF sy-subrc = 0.
WRITE:/ ‘Found’.
ELSE.
WRITE:/ ‘Not found’.
ENDIF.
Output :
CONDANCE
The Condense statement removes blank spaces.
The CONDENSE statement removes blank spaces between the fields, but leaving only 1 character’s space.
‘NO-GAPS’ is an optional addition to the CONDENSE statement that removes all spaces.
Example :
REPORT Z_ABAP_DEVELOPER_TEST.
DATA: lv_title1(10) VALUE ‘SAP ABAP’,
lv_title2(10) VALUE ‘Tutorial’,
spaced_title(30) VALUE ‘SAP ABAP Tutorial’.
CONDENSE spaced_title.
Write: / ‘Condense with Gaps:’, spaced_title.
CONDENSE spaced_title NO-GAPS.
Write: / ‘Condense with No Gaps:’, spaced_title.
Output :
REPLACE
Used to make replacements in characters. Replaces the sub string with another sub string specified, in the main string.
Example :
REPORT Z_ABAP_DEVELOPER_TEST.
DATA: gv_exp1 TYPE string VALUE ‘SAP ABAP & Developers’,
gv_exp2 TYPE string VALUE ‘World’.
REPLACE ‘Developers’ WITH gv_exp2 INTO gv_exp1.
WRITE:/ gv_exp1.
REPLACE ALL OCCURRENCES OF ‘&’ IN gv_exp1 WITH ‘x’.
SKIP 2.
WRITE:/ ‘All & values will be x :’, gv_exp1 .
Output :