String Comparison operators

The following description shows the comparison operators for comparisons between character-type operands.
In ABAP as standard, trailing blanks are taken into account for operands of type string
and are not taken into account for operands of type c, d, n and t.

1.CO(contains Only):

If operand1 contains only the characters from operand2, this comparison is true.
Trailing blanks are taken into account.
This is case sensitive.
If the comparison is false, SY-FDPOS will give the offset of the first character
in operand1 that is not contained in operand2.
If the comparison if true, SY-FDPOS contains the length of operand1.

Examples:
Positive Comparison.

data:char1(4) type c value 'abc ',
char2(4) type c value 'a '.
char2 CO char1.
Result: 2.

Negative comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'ae '.
char2 CO char1.
sy-fdpos: 1.

2.CN(Contains not only):

If the operand1 contains not only the characters of operand 2, this comparison is true.
It is exactly opposite to CO pattern.
If the comparison is true, SY-FDPOS will give the offset of the first character
in operand1 that is not contained in operand2.
If the comparison is false, SY-FDPOS contains the length of operand1.

Examples:
Positive Comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'ae '.
char2 CN char1.
sy-fdpos: 1.

Negative comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'a '.
char2 CN char1.
Result: 2.

3.CA (Contains Any):

If operand1 contains at least one character of operand2, comparison is true.
It is case sensitive.
IF the comparison is true, SY-FDPOS will give the offset of the first character in operand1
that satisfies the comparison.
If the comparison is false, SY-FDPOS will contain the length of operand1.

Example:
Positive Comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'efa '.
char2 CA char1.
Result: 2.

Negative comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'efg'.
char2 CA char1.
Result: 3.

4.NA (contains Not Any):

If operand1 does not contain any of the characters of operand2, comparison is true.
It is case sensitive.
If the comparison is true, SY-FDPOS contains the length of operand1.
If comparison is false, SY-FDPOS contains the offset of the first character of operand1
that occurs in operand2.

Example:

Positive Comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'efg'.
char2 NA char1.
Result: 3.

Negative comparison.

data:
char1(4) type c value 'abc ',
char2(4) type c value 'efb'.
char2 NA char1.
Result: 2.

5.CS (Contains String):

If operand1 contains the string in operand2, the condition is true.
Trailing spaces are ignored and this is not case sensitive.
If the condition is true, SY-FDPOS contains the offset of operand2 in operand1.
If it is false, SY-FDPOS contains the length of operand2.

Example:

Positive Comparison.

data:
char1(4) type c value 'cD',
char2(4) type c value 'abcde'.
char2 CS char1.
Result: 2.

Negative comparison.

data:
char1(4) type c value 'cD',
char2(4) type c value 'acbd'.
char2 CS char1.
Result: 4.

6.NS (contains No String):

If operand1 does not contains the string operand2, comparison is true.
Trailing spaces are ignored and the comparison is not case-sensitive.
If the comparison is true, the system field SY-FDPOS contains the length of operand1.
If the comparison is false, SY-FDPOS contains the offset of operand2 in operand1.

Example:
Positive Comparison:

data:
char1(4) type c value 'cD',
char2(4) type c value 'acbd'.
char2 NS char1.

Result: 4.


Negative comparison.

data:
char1(4) type c value 'cD',
char2(4) type c value 'abcd'.
char2 NS char1.
Result: 2.

7.CP (Contains Pattern):

If operand1 contains pattern operand2, comparison is true.
Trailing spaces are ignored.
Comparison is NOT case sensitive.
If the comparison is true, the system field SY-FDPOS contains the offset of operand2 in operand1.
If it is false, SY-FDPOS contains the length of operand1.

Example:

Positive Comparison.

data:
char1(4) type c value '*c*',
char2(4) type c value 'abcd'.
char2 CP char1.

Result: 2.

Negative comparison.

data:
char1(4) type c value '*&c*',
char2(4) type c value 'acbd'.
char2 CP char1.
Result: 4.

8.NP (contains No Pattern):

If operand1 does not contains pattern operand2, comparison is true.
Trailing spaces are ignored.
Comparison is NOT case sensitive.
If it is true, SY-FDPOS contains the length of operand1.
If the comparison is false, the system field SY-FDPOS contains the offset of operand2 in operand1.

Example:
Positive Comparison.

data:
char1(4) type c value '*&c*',
char2(4) type c value 'acbd'.
char2 CP char1.
Result: 4.

Negative comparison.

data:
char1(4) type c value '*c*',
char2(4) type c value 'abcd'.
char2 CP char1.
Result: 2.

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.