Friday, September 21, 2012

String Operations

CONCATENATE – Combines 2 or more strings into one string.
DATA: s1(10) VALUE 'Hello',
      s2(10) VALUE 'ABAP',
      s3(10) VALUE 'World',
      result1(30),
      result2(30).

CONCATENATE s1 s2 s3 INTO result1.
CONCATENATE s1 s2 s3 INTO result2 SEPARATED BY '-'.

WRITE / result1.
WRITE / result2.
Output
String-Operations-1

If the the concatenated string fits in the result string, then the system variable sy-subrc is set to 0. If the result has to be truncated then sy-subrc is set to 4.
SPLIT – Splits a string into 2 or more smaller strings.
DATA: s1(10), s2(10), s3(10),
      source(20) VALUE 'abc-def-ghi'.

SPLIT source AT '-' INTO s1 s2 s3.

WRITE:/ 'S1 - ', s1.
WRITE:/ 'S2 - ', s2.
WRITE:/ 'S3 - ', s3.
Output
String-Operations-2
If all target fields are long enough and no target fields has to be truncated then sy-subrc is set to 0, else set to 4.
SEARCH – Searches for a sub string in main string. If found then sy-subrc is set to 0, else set to 4.
DATA: string(30) VALUE 'SAP ABAP Development',
      str(10) VALUE 'ABAP'.

SEARCH string FOR str.
IF sy-subrc = 0.
  WRITE:/ 'Found'.
ELSE.
  WRITE:/ 'Not found'.
ENDIF.
Output
String-Operations-3
REPLACE – Replaces the sub string with another sub string specified, in the main string. If replaced successfully then sy-subrc is set to 0, else set to 4.
DATA: string(30) VALUE 'SAP ABAP Development',
      str(10) VALUE 'World'.

REPLACE 'Development' WITH str INTO string.
WRITE:/ string.
Output
String-Operations-4

 

No comments:

Post a Comment