1、獲取SAP特殊字符
sap特殊字符用類cl_abap_char_utilities的靜態變量保存,可以通過該類的靜態變量獲取特殊字符。
例如獲取/n/r回車換行:
DATA _n_r TYPE char2. "回車換行
DATA _n TYPE char1. "換行
DATA _r TYPE char1. "回車
_n_r = cl_abap_char_utilities=>cr_lf. _n = _n_r+0(1). _r = _n_r+1(1).
2、特殊字符在文本中的顯示
DATA lv_msg TYPE string. lv_msg = 'This is first line,' && _n_r && 'separated by "/n/r",' && _n && 'separated by "/n",' && _r && 'separated by "/r".'. WRITE:/ lv_msg.
特殊字符在sap快速顯示中表示為“#”符號:
正確顯示:
3、特殊字符的替換方法
若文本中包含特殊字符,在sap接口數據傳輸中極有可能會因為無法處理特殊字符而使程序意外終止,因此有時需要在傳輸前先去掉sap文本中的特殊字符。
(1)針對某個特定特殊字符進行替換:
替換全部回車換行
REPLACE _n_r WITH ' ' INTO lv_msg. WHILE sy-subrc = 0. REPLACE _n_r WITH ' ' INTO lv_msg. ENDWHILE.
(2)使用sap函數進行替換:
獲取當前語言環境代碼頁編號
DATA codepage TYPE cpcodepage. CALL FUNCTION 'NLS_GET_FRONTEND_CP'
EXPORTING langu = sy-langu IMPORTING frontend_codepage = codepage EXCEPTIONS illegal_syst_codepage = 1 no_frontend_cp_found = 2 internal_or_db_error = 3 OTHERS = 4. WRITE:/ codepage.
中文環境下代碼頁是8404
當前代碼頁下文本中特殊字符替換為空格
CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
EXPORTING intext = lv_msg inter_cp = codepage replacement = 32
IMPORTING outtext = lv_msg EXCEPTIONS invalid_codepage = 1 codepage_mismatch = 2 internal_error = 3 cannot_convert = 4 fields_not_type_c = 5 OTHERS = 6. WRITE:/ lv_msg.
快速顯示文本中表示特殊字符的“#”已經替換為空格
實際顯示效果:
4、附測試代碼:
*&---------------------------------------------------------------------* *& Report ZTEST05 *& *&---------------------------------------------------------------------* *& test by ybin 2021.07.30 *&---------------------------------------------------------------------*
REPORT ztest05. DATA _n_r TYPE char2. "回車換行
DATA _n TYPE char1. "換行
DATA _r TYPE char1. "回車
DATA lv_msg TYPE string. _n_r = cl_abap_char_utilities=>cr_lf. _n = _n_r+0(1). _r = _n_r+1(1). WRITE:/ _n_r, _n, _r. lv_msg = 'This is first line,' && _n_r && 'separated by "/n/r",' && _n && 'separated by "/n",' && _r && 'separated by "/r".'. WRITE:/ lv_msg. * 獲取當前語言環境代碼頁編號
DATA codepage TYPE cpcodepage. CALL FUNCTION 'NLS_GET_FRONTEND_CP'
EXPORTING langu = sy-langu IMPORTING frontend_codepage = codepage EXCEPTIONS illegal_syst_codepage = 1 no_frontend_cp_found = 2 internal_or_db_error = 3 OTHERS = 4. WRITE:/ codepage. * 當當前代碼頁下文本中特殊字符替換為空格
CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
EXPORTING intext = lv_msg inter_cp = codepage replacement = 32
IMPORTING outtext = lv_msg EXCEPTIONS invalid_codepage = 1 codepage_mismatch = 2 internal_error = 3 cannot_convert = 4 fields_not_type_c = 5 OTHERS = 6. WRITE:/ lv_msg.
運行結果: