Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

Insert Space between a sequence of AlphaNumeric Combinations and provision to replace Special Character with space

$
0
0

Requirement:

This FM will convert any string with Alphanumeric combinations to a string seperated by space and an option to replace special characters with space.

For Eg:

ABC23 @DEF GH45 will be converted to ABC 23 DEF GH 45.

 

FM:


ZSPACEBWLETTERNNUMBER:

*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     REFERENCE(I_TEXT) TYPE  STRING
*"     REFERENCE(I_SPLCHARWEG) TYPE  BOOLEAN OPTIONAL" In case If you want to replace special charaacters with space
*"  EXPORTING
*"     REFERENCE(E_TEXT) TYPE  STRING
*"----------------------------------------------------------------------
DATA   result_tab TYPE match_result_tab .
DATA  res TYPE match_result.
DATA   ls_s TYPE CHAR100.
DATA : lv_str1 TYPE string,
             lv_Str2  TYPE string,
             lv_Str  TYPE string,
             lv_temp TYPE string,
             off  TYPE i,
             moff TYPE i,
             mlen TYPE i.


if i_text is not initial.


*  Replacing the special characters in the string with space
clear lv_Str.
if i_splcharweg = 'X'.
       clear ls_s.
       move i_text to ls_s.
       CALL FUNCTION 'ES_REMOVE_SPECIAL_CHARACTER'  " I used the Standard FM for this purpose
         EXPORTING
           TEXT1                      =   ls_s
        IMPORTING
          CORR_STRING        ls_s
                 .
       move ls_s to lv_str.
else.
move i_text to lv_str.
endif.

refresh result_tab.
clear : res,lv_str1,lv_str2,lv_temp,e_text,off,moff,mlen.


lv_str1 = '[a-z][0-9]|[0-9][a-z]'.  " This is the pattern that I will use to find sequence of Alphanumeric Combinations


* Seperating the sequence of Alphanumeric or NumericAlpha combinations with Space

FIND ALL OCCURRENCES OF REGEX lv_str1 in lv_str IGNORING CASE RESULTS result_tab.
if sy-subrc = 0.
moff = 1.
loop at result_tab into res .
   move res-offset to off.
   off = off + moff.
   mlen = strlen( lv_str ).
   mlen = mlen - off.
CONCATENATE lv_str+0(off) lv_str+off(mleninto lv_temp SEPARATED BY space.
clear lv_str.
lv_Str = lv_temp.
   clear res.
moff = moff + 1.
endloop.
endif.
move lv_str to e_text.
CONDENSE e_text. " Removes  multiple spaces in the string
else.
   MESSAGE 'No Input Provided' TYPE 'I'.
endif.
ENDFUNCTION.


Hope this will be helpful.


Thanks & Regards,

Pallavi Andole


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>