The translate command allows you to convert characters within a string from one character to another.
i.e. Convert 28.10.1979 to 28-10-1979 Convert to UPPER/LOWER CASE
*Code to Replace one char with another using TRANSLATE command *------------------------------------------------------------- DATA: ld_date(10) type c. ld_date = '28.10.1979'. *Finds all occurences of first char(.) & replaces them with second(-) TRANSLATE ld_date using '.-'. *ld_date will now contain '28-10-1979'
*Code to demonstrate TRANSLATE to UPPER/LOWER CASE command *--------------------------------------------------------- DATA: ld_char(20) type c. ld_char = 'Hello World'. TRANSLATE ld_char TO UPPER CASE. "Result: ld_char = 'HELLO WORLD' TRANSLATE ld_char TO LOWER CASE. "Result: ld_char = 'hello world'
In ABAP OVERLAY statement is used for overlaying the characters of string on another string. Overlayed string characters will be replaced by the other string characters according to the position.
Example 1:
- Take two strings ,
- string1=ABCDEF
- string2=12345
- Command for OVERLAY is “OVERLAY string1 WITH string2.”
- After this, string1 will becomes 12345F.
- The first 5 characters of string1 have replaced by the string2.
Example 2
- string1=A B C D E F
- string2=12 3 4 5
- After the command OVERLAY string1 WITH string2.
- string1 will becomes 12B3C4D5
- Each character is replaced with the characters of string2 as per the position.
- In this example blank spaces of string1 also replaced.
If we need to replace only characters ( not blank spaces ),we need to used an additional parameter “ONLY str” with OVERLAY statement.
The complete syntax will be :
OVERLAY string1 WITH string2 [ONLY str].
OVERLAY string1 WITH string2 [ONLY str].
REPLACE
REPLACE f WITH g INTO h LENGTH len (length specification for field f )
Replaces the first occurrence of the contents of field f in field h with the contents of field g. All fields are handled in their defined length; this means that closing blanks are not ignored.
The return code value indicates whether the string f was found in h and replaced by g :
SY-SUBRC = 0 String replaced.
SY_SUBRC = 4 String not replaced.
SY_SUBRC = 4 String not replaced.
Example
DATA FIELD(10).
MOVE ‘ABCB’ TO FIELD.
REPLACE ‘B’ WITH ‘string’ INTO FIELD.
MOVE ‘ABCB’ TO FIELD.
REPLACE ‘B’ WITH ‘string’ INTO FIELD.
returns:
FIELD = ‘AstringCB’,
SY-SUBRC = 0
No comments:
Post a Comment