Diving Deeper into ABAP: Exploring String Operations - Concatenate, Split, Condense, and More!
Welcome back everyone, We have discussed the different types of loops and system variables in our previous blog, Today We will continue with String operations.
String Operations :-
- A string is a collection of characters.
- String is an elementary data type of variable length.
String Operations :- CONCATENATE
-
The purpose of concatenate is to combine the strings.
-
Syntax : concatenate <c1> ———— <cn> into <c> separated by <s>.
-
In the above syntax : concatenate = keyword, <c1>————<cn> = individual strings, into = keyword, <c> = final result string, separated by = keyword, <s> = separated.
-
Create a executable program in SE38.
*********************************************************************** *Start of Program *Declaring variables DATA : lv_input1(10) type c value 'Welcome', lv_input2(10) type c value 'To', lv_input3(10) type c value 'Home', lv_output type string. "No need to declare length for string ************************************* *Concatenate the string. CONCATENATE lv_input1 lv_input2 lv_input3 into lv_output SEPARATED BY space. ************************************** *Displaying the output. Write : 'The output is', / lv_output. *End of Program *********************************************************************************
Note :- separated by used to give a space between variables during concatenation.
Output :
String Operations :- SPLIT
- The purpose of split is to divide the strings.
- For split, separator is compulsory.
- Syntax : split<string> at <separator> into <f1><f2><f3>…
- In the above syntax : split = keyword, <string> = string which we need to split, at = keyword, <separator> = any delimiter, into = keyword, <f1><f2><f3>———- = individual strings.
Note :- In the above program during concatenation, we concatenated multiple strings into lv_output at space separator, now we will split them using split.
****************************************************
*Start of program.
*Declaring variables for output
DATA : lv_result1 TYPE string,
lv_result2 TYPE string,
lv_result3 TYPE string.
***************************
*Performing split.
SPLIT lv_output AT space INTO lv_result1 lv_result2 lv_result3.
**************
*Displaying the output.
WRITE : 'The results are :',
/ lv_result1,
/ lv_result2,
/ lv_result3.
*End of program
***********************************************************
Output :
String Operations : CONDENSE
-
The purpose of condense is to remove the leading and trailing spaces and convert a sequence of spaces into a single space.
-
Syntax : condense<c>.
-
In the above syntax : condense = keyword, <c> = string which we want to condense.
********************************************************* *Start of program *Define a variable with alot of leading and trailing spaces DATA : lv_input type string value ' Welcome To HOME '. *Before Aplying Condense. WRITE : 'Before Condense : ', / lv_input. *Apply Condense. CONDENSE lv_input. *After Apply condense Write :/ 'After Condense : ', / lv_input. *End of Program. ***********************************************************
Output :-
String Operations : CONDENSE NO-GAPS :-
-
To remove the entire spaces the addition no-gaps is used with condense.
-
Syntax : condense <c> no-gaps.
-
In the above syntax : condense = keyword, <c> = string which we want to condense, no-gaps = keyword.
********************************************************* *Start of program *Define a variable with alot of leading and trailing spaces DATA : lv_input type string value ' Welcome To HOME '. *Before Aplying Condense. WRITE : 'Before Condense No gaps : ', / lv_input. *Apply Condense. CONDENSE lv_input NO-GAPS. *After Apply condense Write :/ 'After Condense no gaps : ', / lv_input. *End of Program. ***********************************************************
Output :
String Operations :- STRLEN
-
The purpose of STRLEN is to provide the string length.
-
Syntax : len = strlen( string ).
-
In the above syntax : len = variable name which returns the length of the string, strlen = pre-defined operation, string = string whose length needs to be calculated.
****************************************************************** *Start of Program *Defining the variable DATA : lv_input type string value ' Welcome To HOME '. DATA : lv_length(2) type n. *Using strlen lv_length = STRLEN( lv_input ). *Displaying the length WRITE : 'The length of the string is', / lv_length. *End of Program ********************************************************************
Output :-
String Operations : FIND
-
The purpose of find is to find a particular pattern in a string.
-
Syntax : FIND<pattern>IN<str>.
-
In the above syntax : FIND = keyword, <pattern> = is the sequence of characters we are looking for, IN = keyword, <str> = is the string that is being searched.
************************************************************* *Start of Program DATA lv_input(50) type c value 'System Application Product'. FIND 'System' IN lv_input. IF sy-subrc eq 0. WRITE : 'Successful', sy-subrc. ELSE. WRITE : 'Unsuccessful', sy-subrc. ENDIF. *End of Program ***********************************************************
Output :
- Here, sy-subrc 0 indicates that above statement has executed successfully.
So, that’s enough for today.
We will discuss internal table and work area tomorrow.
Thanx alot for being a part of this wonderful journey.
Comments
Post a Comment