Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Obsolete Language Elements →  Obsolete character string and byte string processing 

SEARCH

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Obsolete Syntax

SEARCH dobj FOR pattern [IN {CHARACTER|BYTE} MODE]
       [STARTING AT p1] [ENDING AT p2]
       [ABBREVIATED]
       [AND MARK].

Extras

1. ... IN {CHARACTER|BYTE} MODE

2. ... [STARTING AT p1] [ENDING AT p2]

3. ... ABBREVIATED
4. ... AND MARK

Effect

This statement searches the data object dobj according to the search pattern specified in pattern. The additions allow you to search subareas, shortened patterns and marking of places of finding.

The search ends at the first hit and sy-fdpos is set to the offset of the found pattern or to the word in the search area. If the pattern is not found, then sy-fdpos is set to value 0.

Search Pattern in pattern

The pattern in pattern can have the following forms (upper or lower case is irrelevant in character chain processing):

  • "pat"
    In character chain processing, blanks at the end of the character chain are ignored and wildcard characters (*) are treated in a special way if they are found at the position of the first or last character (see following sections).
  • ".pat."
    Only valid for character chain processing. If a pattern "pat" is enclosed by periods (.) , then exactly the character sequence "pat" is searched for, whereby blanks at the end are accounted for and wildcard characters (*) are not treated as wildcards.
  • "*pat"
    Only valid for character chain processing. If a pattern contains the wildcard character (*) as first character, then a word is searched for (see below) which ends with the character sequence "pat".
  • "pat*"
    Only valid for character chain processing. If a character sequence contains the wildcard character (*) as its last character, a word (see below) is searched for that begins with the character sequence "pat".
  • "*pat*"
    Only valid for character chain processing. If a character sequence contains the wildcard character (*) as both first and last character, then the search is not for a word (see below) that contains "pat", but a word that ends with "pat*".

A word in a character-like data object dobj is defined: Enclosed by non-alphanumerical separators.

During string processing with data objects dobj of fixed length, the closing empty character is taken into account, while with pattern it is not. If pattern is an empty string or is of type c, d, n or t and only contains empty characters, the search is never successful.

System Fields

sy-subrc Meaning
0 The search pattern was found in dobj.
4 The search pattern was found in dobj.


Notes

  • We recommend that you use the new statement FIND instead of SEARCH, wherever possible. The functions of SEARCH have - with the exception of marking the found pattern (addition AND MARK) - been covered by the introduction of regular expressions into the statement FIND. If required, marking after a pattern has been found can be replaced by the statement REPLACEor the built-in functionreplace. Here, the replacement patterns for regular expressions are particularly useful. In contrast to FIND, SEARCH cannot distinguish between upper and lower case, and is must slower when searching large texts.
  • A variant of this statement used for searching tables - SEARCH itab - has also been replaced by a variant of statement FIND.

Addition 1

... IN {CHARACTER|BYTE} MODE

Effect

The optional IN {CHARACTER|BYTE} MODE addition determines whether character string or byte string processing is carried out. If the addition is not specified, character string processing is carried out. Depending on the processing type, dobj and pattern must be either character-like or byte-type.

Addition 2

... [STARTING AT p1] [ENDING AT p2]

Effect

With the additions STARTING AT and ENDING AT, you can restrict the search to a subarea of the data object dobj. For p1 and p2, data objects of the data objects of the data type i are expected.

The value in p1 specifies the first, the value in p2 specifies the last of the positions to be searched. Without specifiying STARTING AT p1, the data object dobj is searched from the first position to position p2. Without specifying ENDING AT p2, dobj is searched from position p1 to the end.

If the addition STARTING AT is specified, then sy-fdpos is set to the offset of the find location minus the offset of p1, provided that the search was successful. In the following cases, the search is not carried out, and sy-subrc is set to 4:

  • The value of p1 or p2 is less than 1.
  • The value of p1 is greater than the length of dobj.
  • The value of p2 is less than or equal to p1.


Note

The term "position" is not equivalent to the term "offset". A byte or a character on position 1 has an offset of 0.

Addition 3

... ABBREVIATED

Effect

With the addition ABBREVIATED, you can specify a shortened pattern in pattern. This addition is only possible with character chain processing. A word is searched in dobj that begins with the same character as the pattern in pattern and contains the remaining characters of "pattern" in the same sequence, but at otherwise completely arbitrary positions of the word.


Example

Search for an abbreviated pattern with SEARCH. The FIND statement has the same result and also specifies the find location.

DATA: text TYPE string VALUE `Roll over Beethoven`, 
      moff TYPE i, 
      mlen TYPE i. 

SEARCH text FOR 'bth' ABBREVIATED. 

FIND REGEX '\<(b[a-z0-9]*t[a-z0-9]*h[a-z0-9]*)\>' IN text 
     IGNORING CASE 
     MATCH OFFSET moff 
     MATCH LENGTH mlen. 

Addition 4

... AND MARK

Effect

With the addition AND MARK, a character sequence found in dobj or a found word, is converted to upper case. This addition is only possible with character chain processing, and you are only allowed to specify changeable data objects for dobj when you use it.


Example

The first two SEARCH-statements have the same effect. They find the first blank in text and set sy-fdpos to the value 4. The third SEARCH statement finds the word "Beethoven" in the search area (beginning from position 6 of text), sets sy-fdpos to the value 5. In other words, the offset of the place of finding in the search area and changes the content of text to "Roll over BEETHOVEN".

DATA: text TYPE string VALUE `Roll over Beethoven`, 
      pos TYPE i. 

SEARCH text FOR '. .'. 
SEARCH text FOR ` `. 

IF sy-subrc = 0. 
  pos = sy-fdpos + 2. 
    SEARCH text FOR 'bth' STARTING AT pos 
                         ABBREVIATED AND MARK. 
ENDIF.