Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Remove or Replace Non-Printable characters from a string

Former Member
0 Kudos

Hi,  

I have a string contains special characters (e.g. HexCode: 0xb). I'd like to remove that non printable characters from the string.Can anyone please let me know how to replace Special Characters from a string

Regards

Priyanka

1 ACCEPTED SOLUTION

michael_kozlowski
Active Contributor
0 Kudos

Hi,

you can use REPLACE with regular expression. Search for this. There are a lot of good examples.

Maybe this link will be helpful

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843...

Regards

5 REPLIES 5

michael_kozlowski
Active Contributor
0 Kudos

Hi,

you can use REPLACE with regular expression. Search for this. There are a lot of good examples.

Maybe this link will be helpful

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843...

Regards

0 Kudos

Hi,

try it like this with replace and regular expression.

DATA: str TYPE string.

CONCATENATE 'Hello World'

            cl_abap_char_utilities=>VERTICAL_TAB

            cl_abap_char_utilities=>CR_LF

            cl_abap_char_utilities=>FORM_FEED

INTO str.

replace all occurrences of regex '[^[:print:]]' in str with ` `.

Br

Michael

SuhaSaha
Advisor
Advisor
0 Kudos

Do see the "0xb" in the hexadecimal viewer of the debugger or is it part of the string?

If latter, then how do you think the ABAP runtime can identify "0xb" as a hex-code? Is there a special character (e.g., , #, ~) which marks the beginning of the hex-values?

BR,

Suhas

Juwin
Active Contributor
0 Kudos

Better try to determine what you would consider as "printable". Once you have that list, you may replace any other character with space.

Thanks,

Juwin

juan_suros
Contributor

Priya,

Sadly, The SAP implementation of REGEX does not yet allow you to define an arbitrary Unicode character (aka code point) for use with the REPLACE command.

So, you will have to cheat. Try this:

DATA: x_b TYPE c LENGTH 1.

DATA: str TYPE string.

str = 'Hello#There'.

PERFORM c2h USING '2300' CHANGING x_b.

REPLACE x_b IN str WITH ''.

The result is string "HelloThere" after replacing the pound (hex 23) character with nothing. This will work with any UTF-16 code point.


I think your "0xB" hex value is the vertical tab codepoint "0B00".



Use this utility subroutine to make this happen:

FORM c2h USING char TYPE c CHANGING hex TYPE c.

     DATA:

       p_hex    TYPE c,

       x_buffer TYPE xstring.

     FIELD-SYMBOLS: <dest> TYPE x.

*   Move character field to a byte string

    MOVE: char TO x_buffer.

*   Convert bytes to characters (not confined to ascii)

    ASSIGN: p_hex TO <dest> CASTING.

    MOVE: x_buffer TO <dest>.

    UNASSIGN: <dest>.

    hex = p_hex.

ENDFORM.