Finding Character Strings in ABAP



Αξιολόγηση Χρήστη:  / 6
ΧειρότεροΚαλύτερο 
Λεπτομέρειες

To search a character field for a particular pattern, use the SEARCH statement as follows:

SEARCH <c> FOR <str> <options>.

The statement searches the field <c> for <str> starting at position <n1>. If successful, the return code value of SY-SUBRC is set to 0 and SY-FDPOS is set to the offset of the string in the field <c>. Otherwise, SY-SUBRC is set to 4.

The search string <str> can have one of the following forms.

<str>

Function

<pattern>

Searches for <pattern> (any sequence of characters). Trailing blanks are ignored.

.<pattern>.

Searches for <pattern>. Trailing blanks are not ignored.

*<pattern>

A word ending with <pattern> is sought.

<pattern>*

Searches for a word starting with <pattern>.

Words are separated by blanks, commas, periods, semicolons, colons, question marks, exclamation marks, parentheses, slashes, plus signs, and equal signs.

<option> in the SEARCH FOR statement can be any of the following:

  • ABBREVIATED

Searches the field <c> for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word and the string <str> must be the same.

  • STARTING AT <n1>

Searches the field <c> for <str> starting at position <n1>. The result SY-FDPOS refers to the offset relative to <n1> and not to the start of the field.

  • ENDING AT <n2>

Searches the field <c> for <str> up to position <n2>.

  • AND MARK

If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case.

View source
DATA STRING(30) VALUE 'This is a little sentence.'.
 
WRITE: / 'Searched''SY-SUBRC''SY-FDPOS'.
ULINE /1(26).
 
SEARCH STRING FOR 'X'.
WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC',
               SY-FDPOS UNDER 'SY-FDPOS'
 
SEARCH STRING FOR 'itt '.
WRITE: / 'itt   ', SY-SUBRC UNDER 'SY-SUBRC',
                   SY-FDPOS UNDER 'SY-FDPOS'
 
SEARCH STRING FOR '.e .'.
WRITE: / '.e .', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'.
 
SEARCH STRING FOR '*e'.
WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'.
 
SEARCH STRING FOR 's*'.
WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'.

Output:

SEARCHED SY-SUBRC SY-FDPOS

X            4        0

itt          0       11

.e .         0       15

*e           0       10

s*           0       17

View source
DATASTRING(30) VALUE 'This is a fast first example.',
POS TYPE I,
OFF TYPE I.
 
WRITE / STRING.
 
SEARCH STRING FOR 'ft' ABBREVIATED.
WRITE: / 'SY-FDPOS:', SY-FDPOS.
 
POS = SY-FDPOS + 2.
SEARCH STRING FOR 'ft' ABBREVIATED STARTING AT POS AND MARK.
WRITE / STRING.
WRITE: / 'SY-FDPOS:', SY-FDPOS.
OFF = POS + SY-FDPOS -1.
WRITE: / 'Off:', OFF.

Output:

This is a fast first example.

SY-FDPOS:    10

This is a fast FIRST example.

SY-FDPOS:    4

Off:        15

Note that in order to find the second word containing 'ft' after finding the word 'fast', you have to add 2 to the offset SY-FDPOS and start the search at the position POS. Otherwise, the word 'fast' would be found again. To obtain the offset of 'first' in relation to the start of the field STRING, it is calculated from POS and SY-FDPOS.

Article from http://help.sap.com


You have no rights to post comments

   
   

     

© Developerpages