10-12-2015 1:43 PM
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
10-12-2015 2:13 PM
Hi,
you can use REPLACE with regular expression. Search for this. There are a lot of good examples.
Maybe this link will be helpful
Regards
10-12-2015 2:13 PM
Hi,
you can use REPLACE with regular expression. Search for this. There are a lot of good examples.
Maybe this link will be helpful
Regards
10-14-2015 10:42 AM
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
10-12-2015 2:44 PM
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
10-13-2015 7:13 PM
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
10-14-2015 12:06 AM
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.