abap文本中的特殊字符(回车符,换行符,制表符等)
作者:互联网
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.
运行结果:
标签:codepage,lv,回车符,abap,&&,msg,制表符,TYPE,特殊字符 来源: https://www.cnblogs.com/ybinlog/p/15078581.html