SAP 中strlen()只能计算字符串的个数,不能计算含有中文字符串的长度,如字符串“SAP大波霸”,strlen('SAP大波霸') = 6,其实真实长度为3+3*2 = 9。
我们可以通过cl_abap_list_utilities=>dynamic_output_length来精确获取字符串长度,下面我写了一个截取字符串长度的函数,供参考:
FUNCTION zotfm001. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(I_STRING) TYPE STRING *" VALUE(I_STRLEN) TYPE I *" EXPORTING *" VALUE(E_STRING1) TYPE STRING *" VALUE(E_STRING2) TYPE STRING *"---------------------------------------------------------------------- DATA:lv_char TYPE string, lv_len TYPE i, lv_st1 TYPE i, lv_st2 TYPE i, lv_str TYPE i. CHECK i_string IS NOT INITIAL AND i_strlen IS NOT INITIAL. lv_str = strlen( i_string ). DO. IF lv_str >= sy-index. lv_char = i_string+0(sy-index). CALL METHOD cl_abap_list_utilities=>dynamic_output_length EXPORTING field = lv_char RECEIVING len = lv_len. IF lv_len >= i_strlen. e_string1 = lv_char. lv_st1 = strlen( lv_char ). lv_st2 = lv_str - lv_st1. e_string2 = i_string+lv_st1(lv_st2). EXIT. ENDIF. ELSE. e_string1 = i_string. e_string2 = ''. EXIT. ENDIF. ENDDO. ENDFUNCTION.