What is String?

Strings are set of characters arranged in sequence. Strings also called as character strings. Character or string data type is used to declare the character strings in ABAP.

Character data type holds alphanumeric characters with minimum length of 1 character and maximum of 65535 characters. Character data type is left justified and spaces padded to right.

Character data type considered as default data type when no data type specified during the declaration. If character data type used in declaration, length of the string should be specified in the declaration itself.

Creating Strings -

In ABAP programming, string variables defined by declaring the data type as Character. The string variables can get initialized in the same declaration or the string can get passed to the variable anywhere in the program execution.

Below is the sample declaration of string variable W_STR with length 15 and initialized with 'HELLO WORLD'.

DATA W_STR(15) TYPE C VALUE 'HELLO WORLD..!'.

Example -

Below example shows how the strings created and used in the ABAP application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_STRING
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_STRING_EXAMPLE.

* Declaring W_STR1 of data type character with length 15
* and initializing with 'HELLO WORLD..!'.
DATA : W_STR1(15) TYPE C VALUE 'HELLO WORLD..!',

* Declaring W_STR2 of data type character with length 15.
       W_STR2(15) TYPE C.

* Assigning 'HELLO WORLD..!' to W_STR2.
W_STR2 = 'HELLO WORLD..!'.

* Displaying both W_STR1, W_STR2 line by line.
WRITE : 'STRING1: ',W_STR1.
WRITE : /'STRING2: ',W_STR2.

Output -

String example output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

W_STR1 variable got assigned with string 'HELLO WORLD..!' at the time of declaration. W_STR2 variable got assigned with string 'HELLO WORLD..!' during the program execution.


Substring processing -

Substring is part of the string or small set of characters from the entire string. In some scenarios, substring needs to be processed. ABAP supports the substring processing by using the string positions.

Syntax -

target_variable 
= source_variable [+] [no_of_positions_from_starting]
		(no_of_characters_length).

  • target_variable - Specifies the target variable.
  • source_variable - Specifies the source variable.
  • + - Mandatory if specifying the no_of_positions_from_starting.
  • no_of_positions_from_starting - Specifies no of characters/positions from the first character in the string. Specifies the starting position of the substring.
  • no_of_characters_length - Specifies the length of the substring.

Example -

Below example shows the substring processing in the ABAP application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_SUBSTRING
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_SUBSTRING.

* Declaring a W_Telephone with full telephone number
DATA  W_Telephone(15) TYPE C VALUE '+91-40-12348765'.

* Declaring different fields for country code,
* area code and phone number.
DATA:  W_Country_code(3) TYPE C,
       W_Area_code(3)    TYPE C,
       W_Phone_num(8)    TYPE C.

* Assigning first three characters from W_Telephone
* to W_Country_code
W_Country_code = W_Telephone(3).

* Assigning 2 characters from 4th position of W_Telephone
* to W_Area_code
W_Area_code    = W_Telephone+4(2).

* Assigning 8 characters from 7th position of W_Telephone
* to W_Phone_num
W_Phone_num    = W_Telephone+7(8).

*
WRITE : /'W_Telephone   : ',W_Telephone,
        /'W_Country_code: ',W_Country_code,
        /'W_Area_code   : ',W_Area_code,
        /'W_Phone_num   : ',W_Phone_num.

Output -

Substring example output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

W_Country_code variable assigned with first 3 characters of W_Telephone. W_Area_code variable assigned with 2 characters from the 5th character of W_Telephone. W_phone_num variable got assigned 8 characters from the 8th character of W_Telephone.


String functions -

Like other programming languages, ABAP provides some built-in string functions to get the character strings information. STRLEN function is the mostly used built-in string function. STRLEN function returns the total number of characters in the string.

Syntax -

Str_length =	STRLEN (String_variable).

  • Str-length - Holds the length of the string returned by STRLEN.
  • String_variable - Contains the string.

Example -

Below example shows how the string length calculated in the ABAP application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_STRLEN
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_STRLEN.

* Declaring W_STR of character data type with 15 length
* and initialized with 'HELLO WORLD..!'.
DATA W_STR(15) TYPE C VALUE 'HELLO WORLD..!'.

* Declaring W_STRL of integer data type.
DATA W_STRL TYPE I.

* Getting the string length and assigning W_STRL.
W_STRL = STRLEN( W_STR ).

* Displaying string and string length.
WRITE : 'String Input: ',W_STR,
       /'String Length: ',W_STRL.

Output -

String functions example

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

W_STRL = STRLEN( W_STR ) - STRLEN calculates the W_STR length and assigns calculated length to W_STRL.


String operations -

ABAP supports various string operations to manipulate the strings in the application program. Below are the list of operations used to manipulate the strings.

OperationDescriptionSyntax
CONCATENATEUsed to join two or more strings to form a third string.

SEPERATED BY Clause used to separate the strings by a specified delimiter.

"sy-subrc" identifies whether the concatenate was successful or not. If "sy-subrc" is zero, then concatenate successful. Otherwise, concatenate not successful.
CONCATENATE W_S1 W_S2 … INTO W_Str [SEPARATED BY W_D].

W_S1, W_S2… - Input strings
W_Str - Output string
W_D - Delimiter
CONDENSEUsed to delete the space characters.

Leave only one-character space in between the characters.

NO-GAPS is optional and used to remove all spaces in between the characters.

"sy-subrc" identifies whether the replace was successful or not. If "sy-subrc" is zero, then replace successful. Otherwise, replace not successful.
CONDENSE W_Str [NO-GAPS].

W_Str - Input & Output string.
REPLACEUsed to replace the characters.

REPLACE replaces the matching first instances of string.

"sy-subrc" identifies whether the replace was successful or not. If "sy-subrc" is zero, then replace successful. Otherwise, replace not successful.
REPLACE W_S1 WITH W_S2 INTO W_Str.

W_S1 - String to replace to
W_S2 - String to replace with
W_Str - Input & Output string.
SEARCHUsed to run searches in character strings.

Two system variables are used for search.

"sy-subrc" identifies whether the search was successful or not. If "sy-subrc" is zero, then search successful. Otherwise, search not successful.

"sy-fdpos" is set to the position of the character string searched if the search is suc¬cessful.
SEARCH W_S1 FOR W_SS.

W_S1 - Input String
W_SS - String to search
SHIFTUsed to move the string content to left or right.

If no addition to the SHIFT statement, the system by default shift every-thing just one character to the left and leaving one space to the right.

The CIRCULAR addition to the SHIFT statement to move shift everything just one character to the left and the character deleted from left places at the right end.
SHIFT W_S1 [LEFT DELETING LEADING W_D]/[CIRCULAR].

W_S1 - Input & output String
W_D - Delimiter
SPLITUsed to split the string content into two or more fields.

"sy-subrc" identifies whether the split was successful or not. If "sy-subrc" is zero, then split successful. Otherwise, split not successful.
SPLIT W_S1 AT W_D INTO W_Str1 W_Str2 ….

W_S1 - Input String
W_D - Delimiter
W_Str1,W_Str2,.. - Output strings

Example -

Below example shows how the string operation used in the ABAP application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_STRING_OPERATIONS
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_STRING_OPERATIONS.

* Declaring required variables for string operations
DATA: W_S1(3)    TYPE C VALUE 'SAP',
      W_S2(4)    TYPE C VALUE 'ABAP',
      W_S3(2)    TYPE C VALUE 'by',
      W_S4(15)   TYPE C VALUE 'TutorialsCampus',
      W_Str(30)  TYPE C,
      W_Str1(30) TYPE C,
      W_SS(4)    TYPE C,
      W_NUM(10)  TYPE C,
      W_I        TYPE I,
      W_D        TYPE C VALUE ' '.

* Concatenate W_S1, W_S2, W_S3, W_S4 to W_Str seperated by space(' ')
CONCATENATE W_S1 W_S2 W_S3 W_S4 INTO W_Str SEPARATED BY W_D.
WRITE : /'Concatenation Output     : ',W_Str.

* Deleting GAPS in between the W_Str
CONDENSE W_Str NO-GAPS.
WRITE : /'Condence NO_GAPS Output  : ',W_Str.

* Concatenate W_S1, W_S2, W_S3, W_S4 to W_Str1 seperated by space(' ')
CONCATENATE W_S1 W_S2 W_S3 W_S4 INTO W_Str1 SEPARATED BY W_D.
*Replacing the first space with ','.
REPLACE W_D WITH ',' INTO W_Str1.
WRITE : /'Replace Output           : ',W_Str1.

* Concatenate W_S1, W_S2, W_S3, W_S4 to W_Str seperated by space(' ')
CONCATENATE W_S1 W_S2 W_S3 W_S4 INTO W_Str SEPARATED BY W_D.
* Searching the 'ABAP' string in the W_Str
W_SS = 'ABAP'.
SEARCH W_Str FOR W_SS.
IF sy-subrc = 0.
  WRITE : /'Search string found at   : ',sy-fdpos.
ELSE.
  WRITE : /'Search string not found'.
ENDIF.

* Shifting the first ZERO from the value '0001234567'
W_NUM = '0001234567'.
SHIFT W_NUM.
WRITE : /'Shift Output             : ',W_NUM.

* Deleting all left ZEROs from '0001234567'.
W_NUM = '0001234567'.
SHIFT W_NUM LEFT DELETING LEADING '0'.
WRITE : /'Shift left delete output : ',W_NUM.

*Shifting left ZERO to Right of the number.
W_NUM = '0123456789'.
SHIFT W_NUM CIRCULAR.
WRITE : /'Shift circular output    : ',W_NUM.

* Concatenate W_S1, W_S2, W_S3, W_S4 to W_Str seperated by space(' ')
CONCATENATE W_S1 W_S2 W_S3 W_S4 INTO W_Str SEPARATED BY W_D.
* Initializing W_S1, W_S2, W_S3, W_S4.
W_S1 = W_S2 = W_S3 = W_S4 = ' '.
* Splitting W_Str to W_S1, W_S2, W_S3, W_S4 based on space as seperator.
SPLIT W_Str AT W_D INTO W_S1 W_S2 W_S3 W_S4.
WRITE :/'SPLIT Output : ',
       /'W_S1  : ',W_S1,
       /'W_S2  : ',W_S2,
       /'W_S3  : ',W_S3,
       /'W_S4  : ',W_S4.

Output -

String operations example

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

CONCATENATE concatenates W_S1, W_S2, W_S3, W_S4 and stores the result in W_Str. CONDENSE removes the spaces from W_Str and produces the output without spaces.REPLACE replaces the first space with comma (,) in the W_Str.

SEARCH string searches for “ABAP” in the string and produced the result. SHIFT produces three different outputs for three different syntaxes and all are self-explanatory. SPLIT splits the W_Str delimited by space, produces 4 outputs which are stored in W_S1, W_S2, W_S3, W_S4.