Thursday, June 26, 2025

πŸ”€ String Operations in SAP ABAP

 

SAP ABAP provides a wide range of string handling operations to manipulate and process character data. Below are some of the most commonly used string manipulation keywords and functions, along with syntax and examples.

1️⃣ SHIFT

The SHIFT keyword shifts the characters of a string to the left or right, removing leading or trailing characters.

πŸ”Ή Syntax:

SHIFT <string> LEFT DELETING LEADING <char>. SHIFT <string> RIGHT DELETING TRAILING <char>.

πŸ”Ή Example:


DATA: str(10) TYPE C VALUE '000001000'. SHIFT str LEFT DELETING LEADING '0'. WRITE: / str.

Output: 1000

πŸ“ Note: The size of the string variable must match the size of the actual string value.

2️⃣ TRANSLATE

The TRANSLATE keyword changes the case of characters in a string (uppercase to lowercase and vice-versa).

πŸ”Ή Syntax:

TRANSLATE <string> TO UPPER CASE.
TRANSLATE <string> TO LOWER CASE.

πŸ”Ή Example:

DATA: str(10) TYPE C VALUE 'SATYA'. TRANSLATE str TO LOWER CASE. WRITE: / str.

Output: satya

3️⃣ REPLACE

The REPLACE keyword substitutes part of a string with another string.

πŸ”Ή Syntax:

REPLACE <old_string> WITH <new_string> INTO <target_string>.

πŸ”Ή Example:

DATA: str(30) TYPE C VALUE 'one thousand rupees'. REPLACE 'rupees' WITH 'dollars' INTO str. WRITE: / str.

Output: one thousand dollars

4️⃣ STRLEN

The STRLEN function returns the length of a string.

πŸ”Ή Syntax:

n = STRLEN( <string> ).

πŸ”Ή Example:

DATA: str(10) TYPE C VALUE 'satya', n TYPE i. n = STRLEN( str ). WRITE: / n.

Output: 5

5️⃣ OFFSET Functionality

Access specific characters or substrings using offset and length.

πŸ”Ή Examples:

1. st = str + 2(1). " Extracts 1 character starting from offset 2 → C 2. st = str + 4(2). " Extracts 2 characters from offset 4 → EF

πŸ“Œ This is useful for substring operations.

6️⃣ SPLIT

The SPLIT keyword breaks a string into multiple parts using a delimiter.

πŸ”Ή Syntax:

SPLIT <string> AT <delimiter> INTO <var1> <var2> ...

πŸ”Ή Example:

DATA: str(10) TYPE C VALUE 'cool-drink', str1(10) TYPE C, str2(10) TYPE C. SPLIT str AT '-' INTO str1 str2. WRITE: / str1, / str2.

Output:

cool drink

7️⃣ CONCATENATE

The CONCATENATE keyword joins two or more strings into one, with or without separators.

πŸ”Ή Syntax:

CONCATENATE <str1> <str2> <str3> INTO <result> SEPARATED BY <delimiter>.

πŸ”Ή Example 1:

DATA: date(10). CONCATENATE SY-DATUM+6(2) SY-DATUM+4(2) SY-DATUM+0(4) INTO date SEPARATED BY '.'. WRITE: / date.

Output: 20.09.2014

πŸ”Ή Example 2:

DATA: year(4), mon(2), day(2), date(10). year = SY-DATUM+0(4). mon = SY-DATUM+4(2). day = SY-DATUM+6(2). CONCATENATE day mon year INTO date SEPARATED BY '.'. WRITE: / date.

Output: 20.09.2014

8️⃣ CONDENSE

The CONDENSE keyword removes unnecessary spaces in a string. When used with NO-GAPS, it removes all spaces.

πŸ”Ή Syntax:

CONDENSE <string> NO-GAPS.

πŸ”Ή Example:

DATA: str(20) TYPE C VALUE 'satya narayana'. CONDENSE str NO-GAPS. WRITE: / str.

Output: satyanarayana

πŸ“ Assignment

Print the following pattern:

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

πŸ’‘ Use nested loops and concatenation to generate the pattern.

No comments:

Post a Comment