0% found this document useful (0 votes)
201 views30 pages

5 SAP ABAP String Processing

This document provides an overview of string processing and formatting in ABAP. Key topics covered include concatenating, condensing, converting case, overlaying, replacing, and searching character strings. Various syntax examples are also given to demonstrate how to concatenate, condense, convert case, overlay, replace substrings, and search for substrings within character strings.

Uploaded by

akash raju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views30 pages

5 SAP ABAP String Processing

This document provides an overview of string processing and formatting in ABAP. Key topics covered include concatenating, condensing, converting case, overlaying, replacing, and searching character strings. Various syntax examples are also given to demonstrate how to concatenate, condense, convert case, overlay, replace substrings, and search for substrings within character strings.

Uploaded by

akash raju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

ABAP Training

String Processing & Formatting

. 1
String Processing

• Concatenating Character Strings


• Condensing Field Contents
• Converting to Upper/Lower Case and Substituting
Characters
• Overlaying Character Fields
• Replacing Field Contents
• Searching for Character Strings
• Shifting
1. Shifting a Field String by a Given Number of Positions
2. Shifting a Field String According to the First or Last Character
3. Shifting a Field String up to a Given String
• Splitting Character Strings
• Obtaining the Length of a Character String

. 2
Concatenating Character Strings

Syntax:

CONCATENATE <c1> ... <cn> INTO <c> [SEPARATED BY <s>].

Examples: Output:

DATA: C1(10) VALUE ‘sum’,


C2(3) VALUE 'mer',
C3(5) VALUE 'holi ',
C4(10) VALUE 'day',
C5(30),
SEP(3) VALUE ' - '.
summerholiday

CONCATENATE C1 C2 C3 C4 INTO C5.


WRITE C5.

CONCATENATE C1 C2 C3 C4 INTO C5 SEPARATED sum - mer - holi - day


BY SEP.
WRITE / C5.

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:

CONDENSE <c> [NO-GAPS].

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.

CONDENSE STRING NO-GAPS.


LEN = STRLEN( STRING ). onetwothreefour !
WRITE: STRING, '!'. Length: 15
WRITE: / 'Length: ', LEN.

. 4
Condensing Field Contents
QUESTION

What if I need an output:


one two three four !
Length: 20

with the following data declaration:


DATA: STRING(25) VALUE
' one two three four',
LEN TYPE I.

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:

TRANSLATE <c> TO UPPER CASE.


TRANSLATE <c> TO LOWER CASE.
TRANSLATE <c> USING <r>.
Examples: Output:
DATA: T(10) VALUE 'AbCdEfGhIj',
STRING LIKE T,
RULE(20) VALUE 'AxbXCydYEzfZ'.

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:

DATA: T(10) VALUE 'a c e g i ',


STRING LIKE T,
OVER(10) VALUE 'ABCDEFGHIJ',
STR(2) VALUE 'ai'.
STRING = T. acegi
WRITE STRING. ABCDEFGHIJ
WRITE / OVER.
aBcDeFgHiJ
OVERLAY STRING WITH OVER.
WRITE / STRING.

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.

REPLACE STR1 WITH STR2 INTO


STRING. abklmnghij
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:

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

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’.

SEARCH STRING FOR 'itt '.


itt 0 11
WRITE: / 'itt ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS’.

SEARCH STRING FOR '.e .'.


WRITE: / '.e .', SY-SUBRC UNDER 'SY-SUBRC', .e . 0 15
SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR '*e'.


WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC', *e 0 10
SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR 's*'. s* 0 17


WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',
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:

WRITE / STRING. This is a fast first example.


SEARCH STRING FOR 'ft' SY-FDPOS: 10
ABBREVIATED.
WRITE: / 'SY-FDPOS:', SY-FDPOS.

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:

SHIFT <c> [BY <n> PLACES] [<mode>].

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:

SHIFT <c> [BY <n> PLACES] [<mode>].

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:

SHIFT <c> LEFT DELETING LEADING <str>.


SHIFT <c> RIGHT DELETING TRAILING <str>.

Examples: Output:

DATA: T(14) VALUE ' abcdefghij


abcdefghij', abcdefghij
STRING LIKE T,
STR(6) VALUE 'ghijkl'.
STRING = T.
WRITE STRING.
SHIFT STRING LEFT DELETING
LEADING SPACE.
WRITE / STRING.

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 '***'.

STRING = ' Part 1 *** Part 2 *** Part Output:


3 *** Part 4 *** Part 5'. Part 1 *** Part 2 *** Part 3 *** Part 4
*** Part 5
WRITE STRING.

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:

[COMPUTE] <n> = STRLEN( <c> ).

Examples: Output:

DATA: INT TYPE I,


WORD1(20) VALUE '12345’,
WORD2(20),
WORD3(20) VALUE
' 4 '.
5
INT = STRLEN( WORD1 ).
WRITE INT.

INT = STRLEN( WORD2 ). 0


WRITE / INT.

INT = STRLEN( WORD3 ). 4


WRITE / INT.

. 18
Formatting Options

. 19
Formatting

• Formatting Output
• Format reset
• Format Color
• Format Input

. 20
Formatting Outputs
Syntax:

WRITE .... <f> <option>.

Examples: Output

DATA: G(5) VALUE 'Hello',


F(5) VALUE 'Dolly'.
Hello Dolby
WRITE: G, F.
Hello
WRITE: /10 G, Dolby
/ F UNDER G.

HelloDolby
WRITE: / G NO-GAP, F.

DATA TIME TYPE T VALUE '154633'.


154633
WRITE: TIME,
15:46:33
/(8) TIME USING EDIT MASK '__:__:__'

WRITE: '000123', 000123


/ '000123' NO-ZERO. 123

. 21
Formatting options for all data types

Option Purpose

LEFT-JUSTIFIED Output is left-justified.


CENTERED Output is centered.
RIGHT-JUSTIFIED Output is right-justified.
UNDER <g> Output starts directly under the field <g>.
NO-GAP The blank after the field <f> is omitted.
USING EDIT MASK <m> Specifies a format template <m>.
NO-ZERO If a field contains only zeros, these are replaced by
blanks.
For type C and N fields, leading zeros are replaced
automatically.

. 22
Formatting options for numeric fields

Option Purpose

NO-SIGN The leading sign is not output.

DECIMALS <d> <d> defines the number of digits after the decimal point.

EXPONENT <e> In type F fields, the exponent is defined in <e>.

ROUND <r> Type P fields are multiplied by 10**(-r) and then


rounded.

CURRENCY <c> Format according to currency <c> in table TCURX.

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

DD/MM/YY Separators as defined in user's master record


MM/DD/YY Separators as defined in user's master record
DD/MM/YYYY Separators as defined in user's master record
MM/DD/YYYY Separators as defined in user's master record
DDMMYY No separators.
MMDDYY No separators.
YYMMDD No separators.

. 24
Format Reset
Syntax:
To set formatting options statically in the program, use the FORMAT statement as follows:

FORMAT <option1> [ON|OFF] <option2> [ON|OFF] ....

To set the formatting options dynamically at runtime, use the FORMAT statement as
follows:

FORMAT <option1> = <var1> <option2> = <var2> ....

To set all formatting options to OFF in one go, use:

Syntax
FORMAT RESET.

. 25
Format, Color (Color in Lists)

Format Options:
• color
• intensified
• inverse

To set colors in the program, use:

Syntax

FORMAT COLOR <n> [ON] INTENSIFIED [ON|OFF] INVERSE [ON|OFF].

To set colors at runtime, use:

Syntax

FORMAT COLOR = <c> INTENSIFIED = <int> INVERSE = <inv>.

Note: Formatting options do not apply to horizontal lines created by ULINE.

. 26
Format, Color (Color in Lists)

<n> <c> Color Intended for

OFF or COL_BACKGROUND 0 depends on GUI background


1 or COL_HEADING 1 grey-blue headings
2 or COL_NORMAL 2 light grey list bodies
3 or COL_TOTAL 3 yellow totals
4 or COL_KEY 4 blue-green key columns
5 or COL_POSITIVE 5 green positive threshold value
6 or COL_NEGATIVE 6 red negative threshold value
7 or COL_GROUP 7 violet group levels

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

FORMAT HOTSPOT [ON|OFF].

To designate fields as hotspots at runtime, use:

Syntax:
FORMAT HOTSPOT = <h>.

Note: 1. You cannot use the HOTSPOT option if INPUT ON is set


2. You cannot format horizontal lines created with ULINE and blank lines created with
SKIP as hotspots.

. 28
Format Input

The output fields can be made input enabled.


Syntax:

FORMAT INPUT [ON|OFF].

. 29
. 30

You might also like