ABAP Keyword Documentation → ABAP - Reference → Program Flow Logic → Expressions and Functions for Conditions → log_exp - Logical Expressions → rel_exp - Predicates → rel_exp - Predicate Functions → Predicate functions for character-like arguments
rel_exp - contains, contains_..., Predicate Functions
Other versions: 7.31 | 7.40 | 7.54
Syntax Forms
-
... contains( val = text sub|start|end = substring [case = case]
[off = off] [len = len] [occ = occ] ) ... -
... contains( val = text regex = regex [case = case]
[off = off] [len = len] [occ = occ] ) ... -
... contains_any_of( val = text sub|start|end = substring
[off = off] [len = len] [occ = occ] ) ... -
... contains_any_not_of( val = text sub|start|end = substring
[off = off] [len = len] [occ = occ] ) ...
Effect
These predicate functions provide a
truth
value for a condition of the argument text
.
- The variants of the function
contains
with the parameterssub
,start
, orend
scan a search range defined byoff
and len intext
for matches with the string specified insubstring
. The return code is true if at least number of occurrences specified inocc
is found. The search is case-sensitive by default, but this can be overridden using the parametercase
. Ifsubstring
is passed tostart
orend
, the matches must occur directly after each other either at the start or at the end of the search range;sub
allows the matches to occur anywhere in the search range. Ifsubstring
is empty, an exception from the class CX_SY_STRG_PAR_VAL is raised.
- The variants of the function
contains
with the parameterregex
scan a search range defined byoff
andlen
intext
for matches with the regular expression specified inregex
. The return code is true if at least number of occurrences specified inocc
is found. The search is case-sensitive by default, but this can be overridden using the parametercase
.
- The function
contains_any_of
has the same effect ascontains
, but does not check for the occurrences of the entire string insubstring
; instead it checks for the individual characters insubstring
. This check is always case-sensitive. The return code is true iftext
contains at least the set of characters specified inocc
. Ifstart
orend
are specified, the characters can be in any order at the start or at the end of the search range.sub
allows them to be anywhere.
- The function
contains_any_not_of
has the same effect ascontains_any_of
, but does not require the characters fromsubstring
; instead it requires any characters that are not insubstring
.
occ
is a
numerical expression position of type i
and its default value is 1. Specifying a value less than or equal to 0 raises an exception of the class CX_SY_STRG_PAR_VAL.
Notes
- The parameter
occ
has a different meaning here than in other functions used for searching.
- The functions shown here can replace the relational operators for character-like data types as follows:
o1 CO o2
|NOT contains_any_not_of( val = o1 sub = o2 )
|
|o1 CN o2
|contains_any_not_of( val = o1 sub = o2 )
|
|o1 CA o2
|contains_any_of( val = o1 sub = o2 )
|
|o1 NA o2
|NOT contains_any_of( val = o1 sub = o2 )
|
|o1 CS o2
|contains( val = to_upper( o1 ) sub = to_upper( o2 ) )
|
|o1 NS o2
|NOT contains( val = to_upper( o1 ) sub = to_upper( o2 ) )
|
Since the relational operators CS
and NS
are not
case-sensitive, the arguments must be processed by the functions
to_upper (or to_lower
). It should also be noted that the trailing
blanks of character-like arguments of fixed length are always ignored in the predicate functions, whereas there are exceptions to this rule for operands of relational operators.
The operators CP
and NP
can be replaced by the
predicate function contains
and a suitable regular expression. The differences
in case handling must also be respected here. Generally, a simple mapping like in CS
and NS
is not possible, but is also not usually required.
Example
The IF
block is reached in the following code excerpt since neither the first
nor the last character in html
occur in the system field sy-abcde
.
DATA html TYPE string.
html = '<body>Text</body>'.
IF contains_any_not_of( val = to_upper( html ) start = sy-abcde ) AND
contains_any_not_of( val = to_upper( html ) end = sy-abcde ).
...
ENDIF.
Exceptions
Catchable Exceptions
CX_SY_RANGE_OUT_OF_BOUNDS
-
Cause: Illegal offset or length specification in the
off
andlen
.
Runtime Error:STRING_OFFSET_TOO_LARGE
CX_SY_REGEX_TOO_COMPLEX
-
Cause: More information: Exceptions in Regular Expressions.
Runtime Error:REGEX_TOO_COMPLEX
CX_SY_STRG_PAR_VAL
-
Cause: Substring in
substring
or regular expression inregex
is empty or occurrence inocc
is less than or equal to 0.
Runtime Error:STRG_ILLEGAL_PAR