有时候会有些类似的字符串:A10010001-A10020001+A10020002+A41030001+A43990001
需要解析为
+
A10010001
-
A10020001
+ A10020002
+ A41030001
+ A43990001
可以参考以下算法:
FORM
zform_get_kemu_r
TABLES
t_right_kemu_list
STRUCTURE
str_right_kemu_list
USING
u_str_hkonts
.
DATA
:
BEGIN
OF
zstr_kume_sign
,
bjh_hkont
TYPE
zbjh_hkont
,
zoperator
TYPE
zoperator
,
END
OF
zstr_kume_sign
.
DATA
:
l_tmp
TYPE
string
,
l_move
TYPE
int4
.
DATA
:
lt_kemu
LIKE
TABLE
OF
zstr_kume_sign
WITH
HEADER
LINE
.
DATA
:
l_wa_kemu_list
LIKE
str_right_kemu_list
.
l_tmp
=
u_str_hkonts
.
"临时保存
REPLACE
ALL
OCCURRENCES
OF
'+'
IN
u_str_hkonts
WITH
'$'
."替换掉加减法符号
REPLACE
ALL
OCCURRENCES
OF
'-'
IN
u_str_hkonts
WITH
'$'
.
WHILE
u_str_hkonts
IS
NOT
INITIAL
.
FIND
'$'
IN
u_str_hkonts
MATCH
OFFSET
l_move
.
"获取科目
IF
sy
-subrc
<>
0
.
"如果只有一项,没有其他的项目加减
lt_kemu
-bjh_hkont
=
u_str_hkonts
.
ELSE
.
lt_kemu
-bjh_hkont
=
u_str_hkonts+0
(l_move
)
.
"如果有多项,
ENDIF
.
"第一个是正
IF
sy
-
index
=
1
.
lt_kemu
-zoperator
=
'+'
.
APPEND
lt_kemu
.
ENDIF
.
"第一个之后
IF
sy
-
index
>
1
AND
u_str_hkonts
IS
NOT
INITIAL
.
lt_kemu
-zoperator
=
l_tmp
(
1
)
.
"获取符号
APPEND
lt_kemu
.
SHIFT
:
l_tmp
BY
1
PLACES
.
"左移一位,去掉符号
ENDIF
.
SHIFT
:
u_str_hkonts
BY
l_move
PLACES
.
"左移,去掉刚才计算过的项目
SHIFT
:
l_tmp
BY
l_move
PLACES
.
SHIFT
:
u_str_hkonts
BY
1
PLACES
.
ENDWHILE
.
"保存解析出的科目和对应的左边科目
LOOP
AT
lt_kemu
.
l_wa_kemu_list
-bjh_hkont
=
lt_kemu
-bjh_hkont
.
l_wa_kemu_list
-operator
=
lt_kemu
-zoperator
.
APPEND
l_wa_kemu_list
TO
t_right_kemu_list
.
ENDLOOP
.
ENDFORM
.
"zform_get_kemu_r