Character String Operators in SAP ABAP

Author | Last Updated| | 0 Comments Facebook Share Twitter Share Google Plus Share A+ A-


Working with character strings in SAP ABAP, using CO, CA, CN, NA and CS in SAP ABAP

In SAP ABAP Character String operators are very useful when writing reports, checking variables and doing ABAP operations on strings, below are the list of character string operators available in SAP ABAP.

Character String Operator Usage, Example
CO (Contains Only) Checks whether a string is solely composed of the characters in a string.
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'sapnuts'.
lv_b = 'sapnuts is a good website'.

IF lv_a CO lv_b.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
CN (Not Contains ONLY) Checks if some string/characters not contains in a string
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'sap courses'.
lv_b = 'sapnuts is a good website'.

IF lv_a CN lv_b.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
CA (Contains ANY) Checks if a string contains at least one character of B
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'sap'.
lv_b = 'sapnuts is a good website'.

IF lv_a CA lv_b.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
NA (NOT Contains Any) Checks if a string does not contain any character of a string
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'course'.
lv_b = 'sapnuts is a good website'.

IF lv_a NA lv_b.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
CS (Contains a String) Check if a string contains another string
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'good website'.
lv_b = 'sapnuts is a good website'.

IF lv_b CS lv_a.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
NS (NOT Contains a String) This is exactly opposite to CS, checks if a string is not contained in another
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'courses'.
lv_b = 'sapnuts is a good website'.

IF lv_b NS lv_a.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
CP (Contains a Pattern) Checks a pattern in a string
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'sapnuts*'.
lv_b = 'sapnuts is a good website'.

IF lv_b CP lv_a.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.
NP (NOT Contains a Pattern) This is exactly opposite to CP
DATA: lv_a TYPE string.
DATA: lv_b TYPE string.

lv_a = 'sapnuts.com*'.
lv_b = 'sapnuts is a good website'.

IF lv_b NP lv_a.
  WRITE:/ 'TRUE'.
ELSE.
  WRITE:/ 'FALSE'.
ENDIF.

Tutorial Comments

Total Comments: Add your Comment

Leave a Comment

Enter Comment Text