Monday, June 11, 2012

0 String Operations In ABAP


String Operations:
String is the variable length data type
Dynamic memory management is used internally i.e the memory allocation at the run time according to the current field content
Note: we can’t declare the string variable through PARAMETER
PARAMETER p_name type String. Is not allowed because the system can’t understand how big it is
Difference between the character and string:
Character
String
Data: v1 type cahr50.
V1 = ‘Career it’.

Here memory is always allocated for 50 characters irrespective of number of characters should be stored currently
Data:v1 type string.
V1 = ‘Career it’.

Here memory is allocate for ‘Career it’ only because always string size depends on number of characters should be stored currently

String operations:
CONCATENATE: to join more than one substring into one variable
Syntax:
CONCATENATE <String1> <string2>………<string N> into <str> separated by <Separator>
Ex :
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.
*//declare varibles
data :str1 type string.
*//concatenating the strings
CONCATENATE 'Career it' 'Ammerpet' 'hyd' INTO str1 SEPARATED BY ','.
*//Displaying output
write:/ str1.
Output:
2. *&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.
*//declare varibles
data :str1 type string.
CONCATENATE 'Career it' 'Ammerpet' 'hyd' INTO str1 SEPARATED BY ','.
*//Displaying output
WRITE:/ str1.
Output:

CONDENSER: replace the sequence of spaces into one space
Ex: CONDENSER ‘Career                it’.
Result: Career it
Ex: CONDENSER ‘Career                it’ NO-GAPS.
Result: Careerit
TRANSLATE: to translate to UPPER / LOWER case
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.
*//declare varibles
data :str1 type string.
* Assigning values to those variables
str1 = 'career it'.

*//Traslate the string into Uppercase
TRANSLATE str1 TO UPPER CASE.
*//Displaying output
write:/ str1.
REPLACE:
Syntax :
REPLACE <str1> with <str2> into <str>.
*&---------------------------------------------------------------------*
*& Report  ZVISHNU_STRING_OPERATIONS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZVISHNU_STRING_OPERATIONS NO STANDARD PAGE HEADING.
*//declare varibles
data :str1 type string,
      str2 
type string,
      str3 
TYPE string.
* Assigning values to those variables
str1 = 
'c'.
str2 = 
'i'.
str3 = 
'Career it'.

*//Replaceing the stings
REPLACE  str2 WITH str1 INTO str3.
*//Displaying output
write:/ str3.
Output:
                      
SPLIT: to split the main string into substrings at the given separator
SPLIT <String> AT <separator> into <str1> <str2>…….<str N>.
*//declare varibles
data :v_id TYPE char20,
      v_name 
TYPE char20,
      v_place 
TYPE char20,
      v_city 
TYPE char20.
*//Calling the split
SPLIT 'c001, Careerit, ammerpet, hyd ' at ',' INTO v_id v_name v_place v_city.
*//Displaying output
write: v_id, v_name, V_place, v_city.
Output:
SHIFT: By default shift to left by one place
SHIFT <str> <LEFT/RIGHT/CIRCULAR> by <N> places. (N>0)
SHIFT <str> LEFT DELETING LEADING <CHAR>
SHIFT <str> RIGHT DELECTING TRAILING <CHAR>.

SEARCH: searches for the required main string in the main string
SEARCH <str> FOR <str1>.
SEARCH string1 FOR string2 STARTING AT position1 ENDING AT position 2.
-       position1 and position2 are starting ending line of internal tables.


Comparing strings:
                Operator
Description
                   CO
Contains only
                   CN
Contains not only
                   CA
Contains any
                   NA
Contains not only
                   CS
Contains String
                    NS
Contains no String
                    CP
Matches pattern
                    NP
Does not Matches pattern

CO(Contains only):
IF <str1> CO <str2>.
Is TRUE if string1 contains only the characters from string2 the comparison is case sensitive including spaces
If the comparison  is true then the system field SY-FDPOS contains the length of string1
If it is false then SY-FDPOS contains the offset of the first character of string1 that does not occur in string2
Examples:
         'AABB' co 'AB' True
         'ABCD' co 'ABC' False
         'AABB' cn 'AB' False
         'ABCD' cn 'ABC' True
         'AXCZ' ca 'AB' True
         'ABCD' ca 'XYZ' False
         'AXCZ' na 'ABC' False
         'ABCD' na 'XYZ' True 
 

SAP-ABAP Copyright © 2011 - |- Template created by Vishnu - |- Powered by Blogger Templates