ABAP字符串按长度拆分

 1 REPORT  ytest_012 MESSAGE-ID oo.
 2 
 3 DATA: l_string(1024)  TYPE c.
 4 DATA: l_string1(1024) TYPE c.
 5 DATA: l_string2(1024) TYPE c.
 6 DATA: l_pos TYPE i.
 7 DATA: BEGIN OF ls_line,
 8   line(100) TYPE c, 
 9 END OF ls_line, 
10 lt_line LIKE TABLE OF ls_line. 
11 
12 l_string = 'asdgtrtfjdsjfiuiowhjhqhbdsnbkjjvkjckl'.
13 
14 DO.
15   CALL FUNCTION 'STRING_SPLIT_AT_POSITION'
16     EXPORTING 
17       string                  = l_string
18       pos                     = 5
19 *   LANGU                   = SY-LANGU 
20    IMPORTING
21      string1                 = l_string1
22      string2                 = l_string
23      pos_new                 = l_pos
24    EXCEPTIONS 
25      string1_too_small       = 1 
26      string2_too_small       = 2 
27      pos_not_valid           = 3 
28      OTHERS                  = 4
29             .
30   IF sy-subrc = 0 .
31 *    WRITE:/ l_string1, '-'. 
32     ls_line-line = l_string1 .
33     APPEND ls_line TO lt_line .
34   ELSEIF sy-subrc = 3 .
35 *    WRITE:/ l_string. 
36     ls_line-line = l_string .
37     APPEND ls_line TO lt_line .
38     EXIT .
39   ENDIF.
40 
41 ENDDO.
42 
43 CLEAR: ls_line .
44 LOOP AT lt_line INTO ls_line.
45 
46   WRITE:/ ls_line-line. 
47 
48 ENDLOOP.
49 
50 
51 " 用下面的这个方法来截,不会把中文截断,而且中文当2个长度(as_character <> 'X')
52 CALL FUNCTION 'TEXT_SPLIT'
53   EXPORTING
54     length       = 3
55     text         = lv_str
56 *    as_character = 'X' 
57   IMPORTING
58     line         = lv_str1
59     rest         = lv_str2 .
60 
61 WRITE:/ lv_str .
62 WRITE:/ lv_str1 .
63 WRITE:/ lv_str2 .

 

你可能感兴趣的:(ABAP字符串按长度拆分)