Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for String Processing →  String Functions →  Description Functions for Character-Like Arguments 

count, count_... - Search Functions

Other versions: 7.31 | 7.40 | 7.54

Syntax Forms

  1. ... count( val = text  {sub = substring}|{ regex = regex} [case = case]
                 [ off = off] [len = len] ) ...

  2. ... count_any_of( val = text  sub = substring
                        [off = off] [ len = len] ) ...

  3. ... count_any_not_of( val = text  sub = substring
                            [ off = off] [len = len] ) ...

Effect

Just like the search functions find and find_..., the search functions count and count_... search text in the entire string text or in a subarea defined using off and len for characters specified in substring or for a match with a regular expression specified in regex. Instead of an offset, they return the number of occurrences.

The return value has the type i.


Example

The result of the following function calls is 1, 3, and 6.

DATA(result1) = count(            val = `xxx123yyy` regex = `\d+` ). 
DATA(result2) = count_any_of(     val = `xxx123yyy` sub   = `123` ). 
DATA(result3) = count_any_not_of( val = `xxx123yyy` sub   = `x`   ).

Executable Example

String Functions, count, find and match

Exceptions

Handleable Exceptions

CX_SY_RANGE_OUT_OF_BOUNDS

  • Cause: Illegal offset or length specified in off and len.
    Runtime error: STRING_OFFSET_TOO_LARGE

CX_SY_REGEX_TOO_COMPLEX

CX_SY_STRG_PAR_VAL

  • Cause: Substring in sub or regular expression in regex is empty or occurrence in occ is 0.
    Runtime error: STRG_ILLEGAL_PAR