Entering content frame

Obsolete Statements for Character Strings Locate the document in its SAP Library structure

CautionThe following statements are obsolete and are only available to ensure compatibility with Releases prior to 4.6 and 6.10. The statements may appear in older programs but should no longer be used.

Search Using SEARCH

The SEARCH statement is the predecessor of the FIND statement. It exists since Release 6.10.
SEARCH should be used in Releases prior to 7.0 only for functions that are not covered by the FIND statement. These include in particular searches with special characters.

Since Release 7.0, the SEARCH functions – with the exception of selecting the sample found (AND MARK addition) have been covered by the inclusion of standard expressions in the FIND statement. If required, selecting a sample after it has been found can be programmed itself through offset/length access.

In contrast to FIND , SEARCH is not case-sensitive and is much slower when searching through large texts.

Example

Decimal points and asterisks are special samples whose rules are described in the keyword documentation.

DATA string(30) TYPE c 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

Example

DATA: TYPE c (30) TYPE c 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.

Assigning Parts of Character Strings Using MOVE – PERCENTAGE

The following variant of the MOVEstatement works only with type c fields:

MOVE c1 TO c2 PERCENTAGE p [RIGHT].

Copies the percentage p of the field c1 left-justified (or right-justified if specified with the RIGHT option) to c2.

The value of p can be a number between 0 and 100. The length to be copied from f1 is rounded up or down to the next whole number.

If one of the arguments in the statement is not type c, the parameter PERCENTAGE is ignored.

Example

DATA c1(10) TYPE c VALUE 'ABCDEFGHIJ',
     c2(10) TYPE c.

MOVE c1 TO c2 PERCENTAGE 40.

WRITE c2.

MOVE c1 TO c2 PERCENTAGE 40 RIGHT.

WRITE / c2.

Output:

ABCD      

      ABCD

Replacing Field Contents Using REPLACE WITH

To replace a string in a field with a different string, use the REPLACE statement.

REPLACE str1 WITH str2 INTO c [LENGTH len].

 

REPLACE sub_string WITH new INTO dobj
                   [IN {BYTE|CHARACTER} MODE]
                   [LENGTH len].


The statement searches the field c  for the first occurrence of the first len positions of the pattern str1. If no length is specified, it searches for the pattern str1 in its full length.

Then, the statement replaces the first occurrence of the pattern str1 in field c  with the string str2. If a length len was specified, only the relevant part of the pattern is replaced.

If the return code value of the system field sy-subrc is set to 0, this indicates that str1  was found in c  and replaced by str2. Another return value shows that nothing was replaced. str1, str2, and len can be variables.

Example

DATA: t(10) TYPE c VALUE 'abcdefghij',
      string LIKE t,
      str1(4) TYPE c VALUE 'cdef',
      str2(4) TYPE c VALUE 'klmn',
      str3(2) TYPE c VALUE 'kl',
      str4(6) TYPE c VALUE 'klmnop',
      len TYPE i VALUE 2.

string = t.
WRITE string.

REPLACE str1 WITH str2 INTO string.
WRITE / string.

string = t.
REPLACE str1 WITH str2 INTO string LENGTH len.

WRITE / string.

string = t.
REPLACE str1 WITH str3 INTO string.

WRITE / string.

string = t.
REPLACE str1 WITH str4 INTO string.

WRITE / string.

The output appears as follows:

abcdefghij

abklmnghij

abklmnefgh

abklghij

abklmnopgh

In the last line, the field string is truncated on the right. The search pattern 'cdef' of length 4 is replaced by 'klmnop' of length 6. Then, the rest of the field string is filled up to the end of the field.

 

Leaving content frame