How to Handle Special Characters in a String in ABAP? The characters separated from 0-9, a-z and A-Z are viewed as unique characters. The explanation is at this point unclear to me, however might be on the grounds that they make visitor appearances in strings, now and again!!, simply joking.
As of late while working in one framework mix project between SAP SRM and SAP Ariba, I was requested to download the connections from Buy Request and Long haul Agreements(LTA(s)) into an application server (AL11) envelope from where the SAP PI point of interaction would pick the records and move them to Ariba servers. This sounds simple, yet the point viable came into the image when we saw that the document names of the connections were having a few exceptional characters which were not permitted at the Ariba side. How to Handle Special Characters in a String in ABAP?
To specify them, extraordinary characters permitted by Ariba stage are: ! ; # $ % and ‘ ‘ ( ) ~ _ ^ @ = , – . what’s more, to plan rationale for this, we ought to consider the way that we really want to consider that the quantity of characters can increment or reduction later.
In this way, we considered 2 methodologies. To depict them at a significant level, the methodologies are:
Make a TVARVC variable and pass every one of the permitted unique characters in equivalent to a string. Then, at that point, make a capability module which channels the approaching string utilizing standard articulations.
Make a custom table and keep up with every one of the exceptional characters which should be permitted in something very similar. Then make a capability module where we should compose the rationale to check every single person of the document name string according to our necessity. Clearly, you can’t keep up with a portion of the extraordinary characters like ! _ and so forth. How to Handle Special Characters in a String in ABAP?
Now, let’s discuss the same one by one.
Approach – 1
Make a Custom Table with any name beginning from Z/Y, for instance, ZSPEC_CHAR_ALLWD, which holds 2 sections MANDT and ALLOWED_SPECIAL_CHAR
The Specialized settings are as per the following:
Upgrade Class: Can be improved – ( character-like )
Make a Table Support Generator(TMG) too to keep up with the characters.
For Testing, we have kept up with just 2 unique characters ‘#’ and ‘$’, which ought to be permitted.
Program lines/logic to handle the same:
"Constants Declarations
CONSTANTS: gc_num TYPE char10 VALUE '0123456789',
gc_uscore TYPE c VALUE '_',
gc_exclaim TYPE c VALUE '!'.
"Data Declarations
DATA: gt_allowed_chars TYPE STANDARD TABLE OF zspec_char_allwd,
gv_abcde_low TYPE sy-abcde,
gv_str_len TYPE i,
gv_counter TYPE i,
gv_uscore TYPE c,
gv_string TYPE sdok_filnm,
gv_replace_space TYPE c LENGTH 2.
"Selection Screen
PARAMETERS: p_string TYPE crmt_ic_email_file_name
DEFAULT TEXT-001. "Text-001 = Wel~!@#$come to SAP@#$%^&*()Yard
"Fetch all the special characters to be allowed.
SELECT * FROM zspec_char_allwd INTO TABLE gt_allowed_chars.
"Get all the english alphabets
gv_abcde_low = sy-abcde.
"convert them to lower case for case sensitive search.
TRANSLATE gv_abcde_low TO LOWER CASE.
*&---------------------------------------------------------------------*
*& - If it is required to replace the space with some other character
*& - for example by underscore, then un-comment the below code.
*&---------------------------------------------------------------------*
CONCATENATE space gc_uscore INTO gv_replace_space SEPARATED BY space.
CONDENSE p_string.
TRANSLATE p_string USING gv_replace_space.
gv_uscore = gc_uscore.
*&---------------------------------------------------------------------*
gv_string = p_string.
"Count the length of the incoming string.
gv_str_len = strlen( p_string ).
"Now, let's check each and every character.
DO gv_str_len TIMES.
"To avoid any dump for the case that might exist as below:
IF gv_counter GT gv_str_len.
EXIT.
ENDIF.
*&---------------------------------------------------------------------*
*& Check1. Allowed Special Character maintained in table ZSPEC_CHAR_ALLWD
*& Check2. Character is between A-Z
*& Check3. Character is between a-z i.e. in lower case.
*& Check4. Character is between 0-9
*& Check5. Character if replaced with underscore, then ignore
*& underscore.
*&---------------------------------------------------------------------*
READ TABLE gt_allowed_chars TRANSPORTING NO FIELDS
WITH KEY allowed_special_char = gv_string+gv_counter(1).
IF NOT ( sy-subrc = 0 OR
gv_string+gv_counter(1) CA sy-abcde OR
gv_string+gv_counter(1) CA gv_abcde_low OR
gv_string+gv_counter(1) CA gc_num OR
gv_string+gv_counter(1) EQ gv_uscore ).
WRITE:/ 'Character ',gv_string+gv_counter(1) COLOR 3,' not allowed.' .
REPLACE gv_string+gv_counter(1) IN gv_string+gv_counter WITH ''.
gv_str_len = gv_str_len - 1.
CONTINUE.
ENDIF.
"Increment the counter.
ADD 1 TO gv_counter.
ENDDO.
CONDENSE gv_string.
WRITE:/'Output : ' COLOR 5 , gv_string.
Let’s execute the program and pass the string
Output:
This way we have attempted to accomplish the necessity of eliminating as well as disregarding a portion of the exceptional characters according to our prerequisite. In the event that they need to incorporate a few other unique characters to be overlooked, they need not change the program, but rather they can simply keep up with a similar in the table. Just in the event that, in the event that some person isn’t getting saved in the table, we want to deal with something very similar by rolling out the improvement in the program rationale.
Approach – 2 – Using Regular expressions
In this methodology, to make the entire idea dynamic, we will simply keep up with the exceptional characters to be permitted in a TVARVC variable. Then in our program, capability module or any place required we will simply bring the TVARVC variable worth and make the customary articulation powerfully.
TVARVC variable – We will permit just ‘#’ and ‘$’ in the variable.
Go to Tcode: STVARV and keep up with the variable as underneath:
"Constants Declarations
CONSTANTS: lc_var_name TYPE rvari_vnam
VALUE 'ELS_ALLOWED_SPECIAL_CHARS',
lc_allowed_chars(11) TYPE c
VALUE '[^0-9a-zA-Z'.
"Data Declarations
DATA: gs_tvarvc TYPE tvarvc,
gv_regex1 TYPE c LENGTH 120 VALUE '[^0-9a-zA-Z]+',
gv_regex_all TYPE c LENGTH 120,
gv_exception TYPE string,
go_root TYPE REF TO cx_root.
*"Selection Screen
PARAMETERS: p_string TYPE crmt_ic_email_file_name
DEFAULT TEXT-001. " p_string = @#$%^&*()Wel~!@#$come to SAP@#$%^&*()Yard
SELECT SINGLE * FROM tvarvc INTO gs_tvarvc
WHERE name = lc_var_name.
WRITE:/ 'String Passed: ' COLOR 3, p_string.
SKIP.
"Create the regular expression.
CONCATENATE lc_allowed_chars gs_tvarvc-low ']+' INTO gv_regex_all.
CONDENSE p_string.
TRY .
"Incase the requirement is to have the first character as alphanumeric only
REPLACE FIRST OCCURRENCE OF REGEX gv_regex1 IN p_string WITH space.
WRITE:/ 'First character should be alphanumeric only' COLOR 4.
WRITE:/ p_string.
"Replace the unwanted special characters from the string with undescore
"ignoring the special characters maintained in TVARVC.
REPLACE ALL OCCURRENCES OF REGEX gv_regex_all IN p_string WITH '_'.
SKIP.
WRITE:/ 'After removing unwanted special characters' COLOR 5.
WRITE:/ p_string.
CATCH cx_root INTO go_root.
gv_exception = go_root->get_text( ).
MESSAGE gv_exception TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.
Output:
How about we execute and see the result:
This way as well, we have attempted to accomplish the necessity of eliminating as well as disregarding a portion of the extraordinary characters according to our prerequisite. In the event that they(users) need to incorporate a few other unique characters to be overlooked, they need not change the program, but rather they can simply keep up with something similar in the TVARVC variable. In this likewise, on the off chance that we need to overlook just highlight or exclamatory image for instance, the standard is similar here too, that they can’t be kept up with alone in a variable, however beneficially, they can be put with different images in something similar.
All things considered, when you will utilize the equivalent through capability module in a program, the outcomes will be case delicate.
This was only one necessity, there can be various untested cases, for them clearly, we could have to change the code according to the prerequisite.
Trust you partook in this blog entry. Be that as it may, I will be more than pleased to accept your valuable criticism. Blissful Learning!!
YOU MAY LIKE THIS
Readymade Business Proposal in hindi dubbed Format
How to check your custom ABAP code for SAP BTP ABAP Environment