ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Expressions and Functions for Conditions → log_exp - Logical Expressions → rel_exp - Comparison Expressions → rel_exp - Relational Operators
rel_exp - Relational Operators for Character-Like Data Types
The following table shows the relational operators for comparisons between character-like operands (single data objects and return values or
string expressions) in
comparison expressions.
Trailing blanks are respected for operands of type string
. If not stated
differently in the following table, trailing blanks are ignored for operands of types c
, d
, n
, and t
.
operator | Meaning |
---|---|
CO |
Contains Only: True if operand1 only contains characters from operand2 .It is case-sensitive and trailing blanks are respected in both operands. If operand2 is of type string and initial, the relational expression is false, exceptif operand1 is also of type string and initial.If operand1 has the type string and is initial,the relational expression is always true, regardless of operand2 . If thecomparison is true, sy-fdpos contains the offset of the first character inoperand1 that is not contained in operand2 . Ifthe comparison is true, sy-fdpos contains the length of operand1 . |
CN |
Contains Not Only: True if a relational expression with CO is false,that is if operand1 contains not only characters from operand2 .sy-fdpos is set in the same way as for CO . Ifthe comparison is true, sy-fdpos contains the offset of the first characterin operand1 that is not contained in operand2 .If the comparison is false, sy-fdpos contains the length of operand1 . |
CA |
Contains Any: True if operand1 contains at least one character from operand2 . It is case-sensitive and trailing blanks are respected in both operands. If operand1 or operand2 are of type string and initial, therelational expression is always false. If result of the comparison is positive, sy-fdpos contains the offset of the first character in operand1 that is also containedin operand2 . If the comparison is false, sy-fdpos contains the length of operand1 . |
NA |
Contains Not Any: True if a relational expression with CA is false, thatis if operand1 does not contain any characters from operand2 .If the comparison is false, sy-fdpos contains the offset of the first characterin operand1 that is also contained in operand2 .If the comparison is true, sy-fdpos contains the length of operand1 . |
CS |
Contains String: True, if the content of operand2 is contained in operand1 . It is not case-sensitive and trailing blanks in the left operand are respected. Ifoperand1 is of type string and initial, or oftype c and contains only blank characters, the relational expression is false,unless operand2 is also of type string and initial,or of type c and only contains blank characters. In this case, the relationalexpression is always true. If the comparison is true, sy-fdpos contains theoffset of operand2 in operand1 . If the comparison is false, sy-fdpos contains the length of operand1 . |
NS |
Contains No String: True if a relational expression with CS is false,that is if operand1 does not contain the content of operand2 .If the comparison is false, sy-fdpos contains the offset of operand2 in operand1 . If the comparison is true, sy-fdpos contains the length of operand1 . |
CP |
Covers Pattern: True if the content of operand1 fits the pattern in operand2 . Wildcard characters can be used to create the operand2 pattern,where "" represents any character string (including a blank string) and"+" represents any character. It is not case-sensitive. Trailing blanksin the left operand are respected. If the comparison is true, sy-fdpos containsthe offset of operand2 in operand1 . Here, leadingwildcard characters "" in operand2 are ignoredif operand2 also contains other characters. If the comparison is false, sy-fdpos contains the length of operand1 . Characters in operand2 can be selected for direct comparisons by prefixing them with theescape character "#". For characters flagged in this way in operand2 , the operatoris case-sensitive. Also, wildcard characters and the escape character are not subject to special handling and trailing blanks are relevant. |
NP |
No Pattern: True if a relational expression with CP is false, that isif operand1 does not fit the pattern operand2 .If the comparison is false, sy-fdpos contains the offset of operand2 in operand1 . Here, leading wildcard characters "*"in operand2 are ignored if operand2 also containsother characters. If the comparison is true, sy-fdpos contains the length of operand1 . |
Other versions: 7.31 | 7.40 | 7.54
Notes
- The operators
CP
andNP
use multiple wildcard characters "*" in a row in the same way as a single "*" character. The wildcard character "+" does not represent a blank string.
- When using the operators
CP
andNP
, there is not usually any point in using patterns without wildcard characters inoperand2
and comparisons with=
can be made instead.
- Operands of byte-like data types cannot be compared using the relational operators from this table. The relational operators for byte-like data types can be used to perform these comparisons for byte-like operands.
- The relational operators in this table can be replaced by predicate functions.
- The statement
FIND
and the search functionsfind
can be quicker than the relational operatorCS
by some magnitude.
- When using relational operators for character-like data types in a
conditional expression or in a
Boolean function, the
system field
sy-fdpos
is given the value set in the expression once the expression is processed.
Example
Searches for HTML tags in a text using the operator CP
. This search finds the first HTML tag "<i>
" at offset 8. Note that it is not enough to specify a search pattern "<>"
,
since CP
stands for Covers Pattern and not for Contains Pattern. The example
also shows that the leading wildcard characters "" in the string are ignored in sy-fdpos
, to identify the occurrence found by this search.
DATA html TYPE string.
html = `This is <i>italic</i>!`.
IF html CP '*<*>*'.
cl_demo_output=>display_text( |Found HTML tag at { sy-fdpos }| ).
ENDIF.
Example
Displays the first position of a letter in a field. The operator CS
is used
in the conditional operator COND
, which returns the content of the system field sy-fdpos
.
cl_demo_output=>display( COND #( WHEN sy-abcde CS 'H' THEN sy-fdpos ) ).
Example
The following comparison is always true, regardless of the input.
DATA(text) = `abcd1234`.
cl_demo_input=>request( CHANGING field = text ).
IF `` CO text.
cl_demo_output=>display( 'true' ).
ENDIF.
Executable Example
Relational Operators for Character-Like Data Types