SAP ABAP 如何处理特殊字符的替换及转义操作

        SAP与外围系统做数据推送时,经常会用到JASON的数据格式。如果数据中有些特殊字符,调用接口时会报一些格式转化的错误。所以需要对一些特使字符进行处理。处理这些字符有两种方式一些特殊字符是没有意义的,例如“回车”、“制表”等字符,对于这里字符作清除处理。一些字符是有意义的,例如'/'  、':',对于这类字符要做转义处理。

    处理方法如下:

    一,清除无意义字符。方法是把无用的字符替换成为空格。参考代码如下:

  DATA: lv_char(10) TYPE c."    	

  lv_char = cl_abap_char_utilities=>horizontal_tab.	"09 TAB符
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>cr_lf.  "0D0A 回车换行
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>vertical_tab.	"0B 垂直制表符
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>newline.  "0A 换行
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>form_feed.  "0C 换页
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>backspace.  "08 退格符
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>byte_order_mark_little.	"(UTF-16LE)的文件头
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

  lv_char = cl_abap_char_utilities=>byte_order_mark_utf8.	"(UTF-8)的文件头
  REPLACE ALL OCCURRENCES OF lv_char IN p_sting WITH ''.

   二,  转义 特殊字符。转义的方法是在特殊字符前加一个 '\'。参考代码如下:

  "  对一些有意义的特殊字符进行转义


  REPLACE ALL OCCURRENCES OF '\' IN p_sting WITH '\\'.   " '\'
  REPLACE ALL OCCURRENCES OF '"' IN p_sting WITH '\"'.   " ':'双引号
  REPLACE ALL OCCURRENCES OF ':' IN p_sting WITH '\:'.   " ':'分号

你可能感兴趣的:(SAP心得,SAP-ABAP,SAP,ABAP,JASON)