又是非Unicode老系统问题,当有中文时,截取就成问题,原因是中文占两字节,而英文只占一个字节,而在老R3系统中C类型只占一个字节。
思路:将字符统一转换成Unicode后截取
DATA: s TYPE string.
s = '1a的的的b1才才才饿饿饿d1是是是e1哦哦哦'.
WRITE:/ s .
WRITE:/ s+14(5)."直接取会出乱码!!
PERFORM frm_substring USING s s 14 5."从第15个字符开始取,取5个字符
WRITE: / s.
*&---------------------------------------------------------------------*
*& Form frm_substring
*&---------------------------------------------------------------------*
* 老R3(非Unicode系统)字符串截
*----------------------------------------------------------------------*
* -->P_SRC text 原字符串
* -->P_SUB text 截取后的字符串
* -->P_off text 起始索引(从0开始)
* -->P_LEN text 截取长度
*----------------------------------------------------------------------*
FORM frm_substring USING p_src
p_sub
p_off
p_len.
DATA: xstr TYPE xstring.
DATA: l_codepage(4) TYPE n .
DATA: l_encoding(20).
CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTING
external_name = 'UTF-16BE'
IMPORTING
sap_codepage = l_codepage.
l_encoding = l_codepage.
DATA: convout TYPE REF TO cl_abap_conv_out_ce.
convout = cl_abap_conv_out_ce=>create( encoding = l_encoding ).
convout->write( data = p_src )."编码
xstr = convout->get_buffer( ).
WRITE: / xstr."
DATA:truc_str TYPE string,off TYPE i,len TYPE i.
truc_str = xstr.
off = p_off * 4.
len = p_len * 4.
truc_str = truc_str+off(len).
WRITE / truc_str."截取获得的二进串
DATA: rf_elem_typ TYPE REF TO cl_abap_elemdescr.
DATA: rf_c TYPE REF TO data.
FIELD-SYMBOLS: <fs_c> TYPE x .
DATA: c_len TYPE i.
c_len = 2 * p_len."Unicode字符,每个占2个字节
rf_elem_typ ?= cl_abap_elemdescr=>get_c( c_len ).
CREATE DATA rf_c TYPE HANDLE rf_elem_typ.
ASSIGN rf_c->* TO <fs_c> CASTING.
<fs_c> = truc_str.
DATA: convin TYPE REF TO cl_abap_conv_in_ce.
convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = <fs_c> ).
DATA: str TYPE string.
CALL METHOD convin->read"解码
IMPORTING data = p_sub.
ENDFORM. " frm_substring