The characters apart from 0-9, a-z and A-Z are considered to be special characters. The reason is still unknown to me, but may be because they make guest appearances in strings, sometimes!!, just kidding.
Recently while working in one system integration project between SAP SRM and SAP Ariba, I was asked to download the attachments of Purchase Order and Long Term Agreements(LTA(s)) into an application server (AL11) folder from where the SAP PI interface would pick the files and transfer them to Ariba servers. This sounds easy, but the topic under consideration came into the picture when we saw that the file names of the attachments were having some special characters which were not allowed at the Ariba side.
To mention them, special characters allowed by Ariba platform are: ! ; # $ % & ‘ ` ( ) ~ _ ^ @ = , – . and to design logic for this, we should consider the fact that we need to consider that the number of characters can increase or decrease later.
Therefore, we thought of 2 approaches. To describe them at a high level, the approaches are:
1. Create a custom table and maintain all the special characters which must be allowed in the same. Then create a function module where we will have to write the logic to check each and every character of the file name string as per our requirement. Obviously, you cannot maintain some of the special characters like ! _ etc.
2. Create a TVARVC variable and pass all the allowed special characters in the same as a string. Then create a function module which filters the incoming string using regular expressions.
New Training Announcement
ABAP RESTful Application Programming on Cloud & on Premise – Starts 6th of Feb 2021
Enrollment Link – ABAP RAP
Course Syllabus – ABAP RAP Content
Training Days – 6, 7, 13, 14, 20, 21, 27, 28 Feb, 6 & 7 Mar 2021 IST
Time: 7:30 AM to 9:30 AM IST (GMT+5.5)
Now, let’s discuss the same one by one.
Approach – 1
Create a Custom Table with any name starting from Z/Y, for example, ZSPEC_CHAR_ALLWD, which holds 2 columns MANDT and ALLOWED_SPECIAL_CHAR

The Technical settings are as follows:

Enhancement Category: Can be enhanced – ( character-like )
Create a Table Maintenance Generator(TMG) as well to maintain the characters.
For Testing, we have maintained only 2 special characters ‘#’ and ‘$’, which should be allowed.

Program lines/logic to handle the same:
<code>"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.</code>
Let’s execute the program and pass the string

Output:

This way we have tried to achieve the requirement of removing as well as ignoring some of the special characters as per our requirement. In case they want to include some other special characters to be ignored, they need not change the program, but they can just maintain the same in the table. Only in case, if some character is not getting saved in the table, we need to handle the same by making the change in the program logic.
Also Read – How can we store underscore in database table with char1 (if we need only one character)?
Approach – 2 – Using Regular expressions
In this approach, to make the whole concept dynamic, we will just maintain the special characters to be allowed in a TVARVC variable. Then in our program, function module or wherever required we will just fetch the TVARVC variable value and create the regular expression dynamically.
TVARVC variable – We will allow only ‘#’ and ‘$’ in the variable.
Go to Tcode: STVARV and maintain the variable as below:

<code>"Constants Declarations CONSTANTS: lc_var_name TYPE rvari_vnam VALUE 'ZSYARD_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.</code>
Output:

Let’s execute and see the output:

This way too, we have tried to achieve the requirement of removing as well as ignoring some of the special characters as per our requirement. In case they(users) want to include some other special characters to be ignored, they need not change the program, but they can just maintain the same in the TVARVC variable. In this also, in case we want to ignore only underscore or exclamatory symbol for example, the rule is the same here as well, that they cannot be maintained alone in a variable, but the good thing is that they can be placed with other symbols in the same.
As far as the output coming in UPPER CASE is concerned, when you will use the same via function module in a program, the results will be case sensitive.
This was just one requirement, there can be numerous untested cases, for them obviously, we might need to adjust the code as per the requirement.
Hope you enjoyed this blog post. But, I will be more than delighted to receive your constructive feedback. Happy Learning!!
Comments, please.
Please follow our LinkedIn Page,LinkedIn Group, Facebook Page, Facebook Group, Twitter , Instagramand Telegram SAP Technical GroupSignal Group.
Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.
Also consider joining SAPYard’s Telegram Channel for SAP Blogs and Tutorials.
Save our number +1-251-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.
Also Check ABAP on Cloud Tutorials
- ABAP on Cloud – 1 – Introduction
- ABAP on Cloud – 2 – ABAP Trial Instance on Cloud
- ABAP on Cloud – 3 – Eclipse Set-up for ABAP Cloud
- ABAP on Cloud – 4 – Your First ABAP Cloud Project
- ABAP on Cloud – 5 – Introduction to RESTFul ABAP Programming
- ABAP on Cloud – 6 – DB Creation and Root View in ABAP Cloud
- ABAP on Cloud – 7 – Insert Records through Class and Create Metadata Extension
- ABAP on Cloud – 8 – Behavior Definition and Service Definition and Binding & Output
- ABAP on Cloud – 9 – Experimenting with Metadata Extension and CRUD Operations
- ABAP on Cloud – 10 – First Fiori Project on Cloud – 1
- ABAP on Cloud – 11 – First Fiori Project on Cloud – 2
- ABAP on Cloud – 12 – First Fiori Project on Cloud – 3
- ABAP on Cloud – 13 – First Fiori Project on Cloud – 4
- ABAP on Cloud – 14 – Advanced Topics – 1 – On Screen Validation for Managed Scenario
- ABAP on Cloud – 15 – Advanced Topics – 2 – On Screen Validation for Managed Scenario
- ABAP on Cloud – 17 – ABAP Service & ABAP Instance in Trial SAP Cloud Platform
- ABAP on Cloud – 18 – Business Application Studio – 1
- ABAP on Cloud – 19 – Business Application Studio – 2