Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow →  Expressions and Functions for Logical Expressions →  Logical Functions →  Predicate Functions →  Predicate functions for character-like arguments 

contains, contains_... - Predicate Functions

Other versions: 7.31 | 7.40 | 7.54

Syntax Forms

  1. ... contains( val = text  sub|start|end = substring [case = case]
                    [off = off] [len = len] [occ = occ] ) ...

  2. ... contains( val = text regex = regex [case = case]
                    [off = off] [len = len] [occ = occ] ) ...

  3. ... contains_any_of( val = text sub|start|end = substring
                           [off = off] [len = len] [occ = occ] ) ...

  4. ... 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 parameters sub, start, or end scan a search range defined by off and len in text for matches with the string specified in substring. The return code is true if at least number of occurrences specified in occ is found. The search is case-sensitive by default, but you can override this with the parameter case. If substring is passed to start or end, 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. If substring is empty, an exception from the class CX_SY_STRG_PAR_VAL is raised.
  • The variants of the function contains with the parameter regex scan a search range defined by off and len in text for matches with the regular expression specified in regex. The return code is true if at least number of occurrences specified in occ is found. The search is case-sensitive by default, but you can override this with the parameter case.
  • The function contains_any_of has the same effect as contains, but does not check for the occurrences of the entire string in substring; instead it checks for the individual characters in substring. This check is always case-sensitive. The return code is true if text contains at least the set of characters specified in occ. If start or end 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 as contains_any_of, but does not require the characters from substring; instead it requires any characters that are not in substring.

occ is a numerical expression position 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.
|Comparison Operator|Predicate Function| |----|----| |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 comparison operators CS and NS are not case-sensitive, the arguments must be processed by the functions to_upper (or to_lower). You should also note that the closing blank characters of character-like arguments of fixed length are always ignored in the predicate functions, whereas there are exceptions to this rule for operands of comparison 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 and len.
    Runtime Error: STRING_OFFSET_TOO_LARGE

CX_SY_REGEX_TOO_COMPLEX

CX_SY_STRG_PAR_VAL

  • Cause: Substring in substring or regular expression in regex is empty or occurrence in occ is less than or equal to 0.
    Runtime Error: STRG_ILLEGAL_PAR