3.9. Manipulation : Strings and Conversions

String manipulation and Conversions

We use mainly ‘&&’(chaining) and ‘|’(pipe) operator to do concatenation but ‘&’ can also be used .


3.9.1.Concatenation


DATA : lv_str1 TYPE char5 VALUE 'uses'.

CONSTANTS : lc_sap TYPE char5 VALUE 'SAP',
                        lc_abap TYPE char5 VALUE 'ABAP'.

DATA(lv1) = |SAP| & | | & |ABAP|.    
* '&' can't be used only with variable like lc_sap & lc_abap is not possible

DATA(lv2) = |SAP| && | | && |ABAP|.

DATA(lv3) = 'SAP' & ' ' & 'ABAP'.
 
DATA(lv4) = lc_sap && ' ' && ‘no space’ && lc_abap.    
" no space even though space is passed

DATA(lv5) = |{ lc_sap } { lc_abap } { lv_str1 } advanced open SQL|.
" my personal favorite known as embedded concatenation
 

Output :

 

3.9.2. Define width, pad characters , set alignment


DATA : lv_str2 TYPE char5 VALUE 'SAP'.


   WIDTH : Can change or set length, 
    PAD :      Use to add extra character at last ,
    ALIGN : LEFT, RIGHT or CENTER


DATA(lv6) = |{ lv_str2 WIDTH = '10' pad = 'A' ALIGN = LEFT  }| .

Output :

3.9.3. Define sign position


DATA : lv_str3 TYPE int4 VALUE '-407'.

DATA(lv7) = |{ lv_str3 SIGN = LEFT }|. " Sign to left , by default its right 

Output :


3.9.4. Replace keyword

DATA : lv_str4 TYPE string VALUE 'SAP ABAP used 7.4 version'.

REPLACE '7.4' WITH '7.51' INTO lv_str4.

Output :

3.9.5. Alpha conversion :  
          now no need of calling conversion exits


DATA : lv_str5 TYPE char10 VALUE '0407'.

DATA(lv8) = |{ lv_str5 ALPHA = in }|.
DATA(lv9) = |{ lv_str5 ALPHA = OUT }|.

Output :



 

3.9.6. Changing case

DATA(lv_upper) = |{ lv_str4 CASE = UPPER }| .

" Case values : Upper , lower or raw

Output :


 

3.9.7. Changing currency

DATA(lv_curr) = |{ lv_str3 CURRENCY = 'USD' NUMBER = USER }|.

Output :



3.9.8. Changing date or time format - we have 
          'RAW',  'ISO', ENVIRONMENT' and 'USER' format


DATA(lv_date) = |{ sy-datum DATE = USER }|.   " SY-UZEIT is used for time conversion

Output :


Next - Conditional operators 

No comments:

Post a Comment