String Commands in ABAP
Below are some of the common and handy commands for manipulating strings in ABAP.
To join multiple strings together into one string.
DATA u(10) TYPE C VALUE 'Rust'. DATA v(10) TYPE C VALUE 'Iron'. DATA w(10) TYPE C VALUE 'Brown'. DATA str(30) TYPE C. CONCATENATE u v w INTO str. CONCATENATE u v w INTO str SEPARATED BY '***'.
To separate a given string into multiple sub strings
DATA str(30) TYPE C VALUE 'Rust Iron Brown'. DATA: u(10) TYPE C, v(10) TYPE C, w(10) TYPE C. SPLIT str AT ',' INTO u v w. WRITE: / u, / v, / w.
To convert a given string from one form to another form
TRANSLATE str TO LOWER CASE. TRANSLATE str TO UPPER CASE. TRANSLATE str USING 'AXBY'.
in the above example, all occurrences of A in str are replaced by X and B in str are replaced by Y.
to search for substring in a main string.
SEARCH str for 'AD'. // it is not case sensitive.
In the above example if substring ‘AD’ is existed in main string str then SY-SUBRC will become 0 otherwise
SY-SUBRC will become 4.
SY-FDPOS maintains the offset value of the substring to search for substring in a main string.
To search for substring in a main string.
FIND 'AD' IN str.
It is case sensitive.
SY-FDPOS doesn’t work.
OFFSET CONCEPT IN STRINGS
- This concept is used to extract or modify substrings of a given main string
- OFFSET indicates position of a character in a string
- OFFSET of 1st character is 0.
- OFFSET of 2nd character is 1 and so on.
DATA W(20) TYPE C VALUE 'INFINITY'. WRITE W+2(3). //here the output is 'FIN'.
this is the example of extracting substring from the main string here 2 indicates OFFSET of the substring and 3 indicates number of characters.
remember here ‘+’ is not an addition operator. Observe no spaces in both the sides.
W+3(2) = '99'. WRITE W.
Here the output is INF99ITY
This is an example for setting substring of main string.
Courtesy: ABAP technical documentation