Replace in ABAP Strings



User Rating:  / 3
PoorBest 
Details

Pattern-based replacement

1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern
IN [section_of] dobj WITH new
[IN {BYTE|CHARACTER} MODE]
[{RESPECTING|IGNORING} CASE]
[REPLACEMENT COUNT rcnt]
{ {[REPLACEMENT OFFSET roff]
[REPLACEMENT LENGTH rlen]}
| [RESULTS result_tab|result_wa] }.

Position-based replacement

2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
[IN {BYTE|CHARACTER} MODE].

 Effect

This statement replaces characters or bytes of the variable dobj by characters or bytes of the data object new. Here, position-based and pattern-based replacement are possible.

 When the replacement is executed, an interim result without a length limit is implicitly generated and the interim result is transferred to the data object dobj. If the length of the interim result is longer than the length of dobj, the data is cut off on the right in the case of data objects of fixed length. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or hexadecimal zeroes. Data objects of variable length are adjusted. If data is cut off to the right when the interim result is assigned, sy-subrc is set to 2.

In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new.

System fields

sy-subrc Meaning
0 The specified section or subsequence was replaced by the content of new and the result is available in full in dobj.
2 The specified section or subsequence was replaced in dobj by the contents of new and the result of the replacement was cut off to the right.
4 The subsequence in sub_string was not found in dobj in the pattern-based search.
8 The data objects sub_string and new contain double-byte characters that cannot be interpreted.


REPLACE pattern IN

Syntax

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern

IN [section_of] dobj WITH new
[IN {BYTE|CHARACTER} MODE]
[{RESPECTING|IGNORING} CASE]
[REPLACEMENT COUNT rcnt]
{ {[REPLACEMENT OFFSET roff]
[REPLACEMENT LENGTH rlen]}
| [RESULTS result_tab|result_wa] }.

Extras:

1. ... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF

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

3. ... {RESPECTING|IGNORING} CASE

4. ... REPLACEMENT COUNT rcnt

5. ... REPLACEMENT OFFSET roff

6. ... REPLACEMENT LENGTH rlen

7. ... RESULTS result_tab|result_wa

Effect

When making replacements based on patterns, the system searches the data object dobj for the byte or character string specified by pattern and replaces the target location with the content of the data object new. If a regular expression is used in pattern, special replacements patterns can be used in new that permit a reference to each target location.

The syntax and effect of the addition section_of are the same as searching for a substring in a data object using the statement FIND, and the same treatable exception can occur.

Note

The REPLACE IN TABLE statement is available for replacements in internal tables.

Addition 1

... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF

Effect

The optional {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF addition specifies whether only the first or all occurrences of the search pattern are replaced by the content of new. If the FIRST OCCURENCE addition or none of the additions is specified, the first occurrence is replaced. In the case of ALL OCCURRENCES, all non-overlapping occurrences are replaced.

 If sub_string in pattern is an empty string or is of type c, d, n, or t and only contains empty characters, the position before the first character or byte of the search area is located when the first occurrence is being searched for, and the content of new is inserted in front of the first character. If all occurrences are being searched for, the CX_SY_REPLACE_INFINITE_LOOP exception is triggered in this case.

If regex in pattern contains a regular expression that is the same as the empty character string, the content of new is also inserted before the first character when the first occurrence is being searched for. When all occurrences are being searched for, the content of new is inserted in this case in front of the first character, in all spaces that do not come before or inside a match, and after the last character.

Example

After replacement, text contains value "x-xx-x".

DATA text TYPE string VALUE '-uu-'.

REPLACE ALL OCCURRENCES OF REGEX 'u*' IN text WITH 'x'.

Addition 2

... IN {BYTE|CHARACTER} MODE

Effect

The optional IN {BYTE|CHARACTER} MODE addition determines whether byte or character string processing is carried out. If the addition is not specified, character string processing is carried out. Depending on the processing type, dobj, new and sub_string in pattern have to be byte-type or character-type. If regular expressions are used in pattern, only character string processing is permitted.

Note

For replacement with character-type content that contains trailing blank characters, new has to have the data type string.

Addition 3

... {RESPECTING|IGNORING} CASE

Effect

This addition is only permitted for processing character strings. It has the same syntax and effect as the corresponding addition for searching for a substring in a data object using the statement FIND. When an instance of the CL_ABAP_REGEX class is used, this addition is not permitted.

Addition 4

... REPLACEMENT COUNT rcnt

Effect

This addition causes the number of replacements made within the data object dobj to be assigned to the data object rcnt. A variable of the type i is expected for rcnt. If no replacement takes place, rcnt is set to 0.

Addition 5

... REPLACEMENT OFFSET roff

Effect

This addition stores the offset relating to the data object dobj, of the found location that was last replaced in dobj in the data objectroff, after all replacements. A variable of the data type i is expected for roff. If there is no replacement, roff retains its previous value.

Addition 6

... REPLACEMENT LENGTH rlen

Effect

This addition stores that length of the last section replaced in dobj, in the data object rlen. A variable of the data type i is expected for rlen. If no replacement is carried out, rlen retains its previous value.

Note

In data objects of fixed length, the number of replacements made within the data object dobj can be smaller than the number of found locations, and the length of the last replaced section can be smaller than the length of new, if the intermediate result is truncated.

Addition 7

... RESULTS result_tab|result_wa

Effect

If at least one replacement is made, the RESULTS addition saves the offsets of the replaced positions and the lengths of the found sub-strings, either in an internal table result_tab or in a structure result_wa. The syntax and meaning of the addition are the same as with the FIND statement, with the exception that the data types for result_tab and result_wa have to be REPL_RESULT_TAB or REPL_RESULT here, for which there is no SUBMATCHES component.

Example

Pattern-based replacement of all found locations of the word "know" in the data objects text1 and text2 by "should know that". After the first REPLACE statement, text1 contains the complete content "I should know that You should know that ", and sy-subrc contains the value 0. The data objects cnt, off, and len contain the values 2, 23, and 16 respectively. After the second REPLACE statement, text2 contains the truncated content "I should know that" and sy-subrc contains the value 2. The data objects cnt, off, and len contain the values 1, 2, and 16.

View source
DATA: text1 TYPE string, 
text2(18) TYPE c, 
cnt TYPE i, 
off TYPE i, 
len TYPE i.
 
text1 = text2 = 'I know you know'.
 
REPLACE ALL OCCURRENCES OF 'know' IN: 
text1 WITH 'should know that' 
REPLACEMENT COUNT cnt 
REPLACEMENT OFFSET off 
REPLACEMENT LENGTH len, 
text2 WITH 'should know that' 
REPLACEMENT COUNT cnt 
REPLACEMENT OFFSET off 
REPLACEMENT LENGTH len. 



REPLACE SECTION OF

Syntax

REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
[IN {BYTE|CHARACTER} MODE].

Effect

When replacing position-based, the section in dobj is replaced starting at the offset specified in off for the length specified in <>ABlen with the content of data object new. For off and len, data objects of data type i are expected. You must specify at least one of the additions OFFSET or LENGTH. If you specify an offset but no length, the content of data object dobj is replaced starting at the offset off to the end. If you specify a length but no offset, the offest 0 is implicitly used. The values of off and len must be greater than or equal to 0 and the section specified by off and len must lie within the length of dobj.

Addition

... IN {BYTE|CHARACTER} MODE

Effect

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

Example

Use the FIND statement to determine offset and length of the first word "know" in the data objects text1 and text2 and to replace this section position-based in text1 and text2 by "should know that". After the first REPLACE statement, text1 contains the complete content "I should know that you know" and sy-subrc contains 0. After the second REPLACE statement, text2 contains the truncated content "I should know that" and sy-subrc contains 2.

View source
DATA: text1 TYPE string, 
text2 TYPE c LENGTH 18, 
off TYPE i, 
len TYPE i.
 
text1 = text2 = 'I know you know'.
 
FIND 'know' IN text1 MATCH OFFSET off 
MATCH LENGTH len.
 
REPLACE SECTION OFFSET off LENGTH len OF: 
text1 WITH 'should know that', 
text2 WITH 'should know that'

Article from : http://help.sap.com


You have no rights to post comments

   

Login  

   

     

© Developerpages