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
containswith the parameterssub,start, orendscan a search range defined byoffand len intextfor matches with the string specified insubstring. The return code is true if at least the number of occurrences specified inoccis found. The search is case-sensitive by default, but this can be overridden using the parametercase. Ifsubstringis passed tostartorend, the matches must occur directly after each other either at the start or at the end of the search range.sub, on the other hand, allows the matches to occur anywhere in the search range. Ifsubstringis empty, an exception from the class CX_SY_STRG_PAR_VAL is raised.
- The variants of the function
containswith the parameterregexscan a search range defined byoffandlenintextfor matches with the regular expression specified inregex. The return code is true if at least the number of occurrences specified inoccis found. The search is case-sensitive by default, but this can be overridden using the parametercase.
- The function
contains_any_ofhas the same effect ascontains, but does not check for the occurrences of the entire string insubstring. Instead, it checks for the individual characters in substring. This check is always case-sensitive. The return code is true iftextcontains at least the set of characters specified inocc. Ifstartorendare specified, the characters can be in any order at the start or at the end of the search range.suballows them to be anywhere.
- The function
contains_any_not_ofhas 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
numeric 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
occhas a different meaning here than in other functions used for searches.
- 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 ) )|
The relational operators CS and NS are not case-sensitive,
which means that 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 section, 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
Handleable Exceptions
CX_SY_RANGE_OUT_OF_BOUNDS
-
Cause: Illegal offset or length specified in
offandlen.
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: A substring in
substringor regular expression inregexis empty or the number of occurrences inoccis less than or equal to 0.
Runtime error:STRG_ILLEGAL_PAR