Professional Documents
Culture Documents
5 SAP ABAP String Processing
5 SAP ABAP String Processing
. 1
String Processing
. 2
Concatenating Character Strings
Syntax:
Examples: Output:
Note: If the result fits into <c>, SY-SUBRC is set to 0. However, if the result has to be truncated,
SY-SUBRC is set to 4.
. 3
Condensing Field Contents
Syntax:
Examples: Output:
DATA: STRING(25) VALUE
' one two three four',
LEN TYPE I.
one two three four !
LEN = STRLEN( STRING ). Length: 25
WRITE: STRING, '!'.
WRITE: / 'Length: ', LEN.
CONDENSE STRING.
LEN = STRLEN( STRING ).
one two three four !
WRITE: STRING, '!'.
Length: 18
WRITE: / 'Length: ', LEN.
. 4
Condensing Field Contents
QUESTION
Code:
CONDENSE STRING.
CONCATENATE STRING ‘!’ INTO STRING SEPARATED BY SPACE.
LEN = STRLEN( STRING ).
WRITE: STRING.
WRITE: / 'Length: ', LEN.
. 5
Converting to Upper/Lower Case and Substituting Characters
Syntax:
STRING = T. AbCdEfGhIj
WRITE STRING.
TRANSLATE STRING TO UPPER CASE. ABCDEFGHIJ
WRITE / STRING.
STRING = T.
abcdefghij
TRANSLATE STRING TO LOWER CASE.
WRITE / STRING.
STRING = T.
xXyYzZGhIj
TRANSLATE STRING USING RULE.
WRITE / STRING.
. 6
Overlaying Character Fields
Syntax:
OVERLAY <c1> WITH <c2> [ONLY <str>]
Examples: Output:
STRING = T.
OVERLAY STRING WITH OVER ONLY STR. AcegI
WRITE / STRING.
Note: 1) If at least one character in <c1> was replaced, SY-SUBRC is set to 0. In all other cases,
SY-SUBRC is set to 4.
2) If <c1> is longer than <c2>, it is overlaid only in the length of <c2>.
. 7
Replacing Field Contents
Syntax:
REPLACE <str1> WITH <str2> INTO <c> [LENGTH <l>].
Examples: Output:
DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T,
STR1(4) VALUE 'cdef',
STR2(4) VALUE 'klmn',
STR3(2) VALUE 'kl',
STR4(6) VALUE 'klmnop',
LEN TYPE I VALUE 2.
STRING = T. abcdefghij
WRITE STRING.
STRING = T.
REPLACE STR1 WITH STR2 INTO STRING LENGTH LEN.
WRITE / STRING. abklmnefgh
Note: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>.
. 8
Replacing Field Contents
Syntax:
REPLACE <str1> WITH <str2> INTO <c> [LENGTH <l>].
Examples: Output:
DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T,
STR1(4) VALUE 'cdef',
STR2(4) VALUE 'klmn',
STR3(2) VALUE 'kl',
STR4(6) VALUE 'klmnop',
LEN TYPE I VALUE 2.
STRING = T.
REPLACE STR1 WITH STR3 INTO abklghij
STRING.
WRITE / STRING.
STRING = T.
REPLACE STR1 WITH STR4 INTO abklmnopgh
STRING.
WRITE / STRING.
Note: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>.
. 9
Searching for Character Strings
Syntax:
Note: 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.
. 10
Searching for Character Strings
Examples:
DATA STRING(30) VALUE 'This is a little sentence.'.
WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.
ULINE /1(26). Output:
SEARCH STRING FOR 'X'. SEARCHED SY-SUBRC SY-FDPOS
WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC', X 4 0
SY-FDPOS UNDER 'SY-FDPOS’.
. 11
Different options
1. ABBREVIATED
2. STARTING AT <n1>
3. ENDING AT <n2>
4. AND MARK
Examples:
DATA: STRING(30) VALUE 'This is a fast first example.',
POS TYPE I,
OFF TYPE I.
Output:
POS = SY-FDPOS + 2.
SEARCH STRING FOR 'ft'
ABBREVIATED STARTING AT POS This is a fast FIRST example.
AND MARK. SY-FDPOS: 4
WRITE / STRING.: / 'SY-FDPOS:', SY- Off: 15
FDPOS.
OFF = POS + SY-FDPOS -1.: / 'Off:', OFF.
. 12
Shifting a Field String by a Given Number of Positions
Syntax:
Examples: Output:
DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T. abcdefghij
STRING = T. bcdefghij
WRITE STRING.
SHIFT STRING.
WRITE / STRING.
STRING = T.
WRITE / STRING.
SHIFT STRING BY 3 PLACES
LEFT. abcdefghij
WRITE / STRING. defghij
STRING = T.
WRITE / STRING.
SHIFT STRING BY 3 PLACES abcdefghij
RIGHT. abcdefg
WRITE / STRING.
. 13
Shifting a Field String by a Given Number of Positions
Syntax:
Examples: Output:
DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T. abcdefghij
STRING = T. defghijabc
WRITE STRING.
SHIFT STRING BY 3 PLACES
CIRCULAR.
WRITE / STRING.
. 14
Shifting a Field String According to the First or Last Character
Syntax:
Examples: Output:
STRING = T.
WRITE STRING. abcdefghij
SHIFT STRING RIGHT DELETING abcdef
TRAILING STR.
WRITE / STRING.
. 15
Shifting a Field String up to a Given String
Syntax:
SHIFT <c> UP TO <str> <mode>.
Examples:
DATA: T(10) VALUE 'abcdefghij',
STRING LIKE T,
STR(2) VALUE 'ef'.
Output:
STRING = T.
abcdefghij
WRITE STRING.
efghij
SHIFT STRING UP TO STR.
WRITE / STRING.
STRING = T.
SHIFT STRING UP TO STR LEFT.
WRITE / STRING. efghij
STRING = T. abcdef
SHIFT STRING UP TO STR RIGHT.
WRITE / STRING.
STRING = T.
SHIFT STRING UP TO STR CIRCULAR. efghijabcd
WRITE / STRING.
Note: If <str> is not found in <c>, SY-SUBRC is set to 4 and <c> is not shifted. Otherwise, SY-
SUBRC is set to 0.
. 16
Splitting Character Strings
Syntax:
SPLIT <c> AT <del> INTO <c1> ... <cn>.
SPLIT <c> AT <del> INTO TABLE <itab>.
Examples:
DATA: STRING(60),
P1(20) VALUE '++++++++++++++++++++',
P2(20) VALUE '++++++++++++++++++++',
P3(20) VALUE '++++++++++++++++++++',
P4(20) VALUE '++++++++++++++++++++',
DEL(3) VALUE '***'.
Part 1
SPLIT STRING AT DEL INTO P1 P2
P3 P4. Part 2
WRITE / P1. Part 3
WRITE / P2. Part 4 *** Part 5
WRITE / P3.
WRITE / P4.
. 17
Obtaining the Length of a Character String
Syntax:
Examples: Output:
. 18
Formatting Options
. 19
Formatting
• Formatting Output
• Format reset
• Format Color
• Format Input
. 20
Formatting Outputs
Syntax:
Examples: Output
HelloDolby
WRITE: / G NO-GAP, F.
. 21
Formatting options for all data types
Option Purpose
. 22
Formatting options for numeric fields
Option Purpose
DECIMALS <d> <d> defines the number of digits after the decimal point.
UNIT <u> The number of decimal places is fixed according to the unit <u>specified in
table T006 for type P
fields.
. 23
Formatting options for date fields
Option Purpose
. 24
Format Reset
Syntax:
To set formatting options statically in the program, use the FORMAT statement as follows:
To set the formatting options dynamically at runtime, use the FORMAT statement as
follows:
Syntax
FORMAT RESET.
. 25
Format, Color (Color in Lists)
Format Options:
• color
• intensified
• inverse
Syntax
Syntax
. 26
Format, Color (Color in Lists)
Note:
• The default setting is COLOR OFF.
• The default color for all text is COL_BACKGROUND
• The default setting is INTENSIFIED ON.
• For COLOR OFF INVERSE has no effect.
. 27
Outputting Fields as HOTSPOTS
To output areas as hotspots, use the following option of the FORMAT statement:
Syntax
Syntax:
FORMAT HOTSPOT = <h>.
. 28
Format Input
. 29
. 30