文章目录
- SHIFT-字符串移位 和 CONDENSE去除空格
- SHIFT BY n PLACES RIGHT/LEFT
- 运行结果
- SHIFT ... UP TO ...
- 运行结果
- 其他的-变量后面加括号和数字
- SHIFT c LEFT/RIGHT DELETING
- 运行结果
- SHIFT 去除0
- 示例程序1
- 运行结果
- 示例程序2
- 运行结果
- CONDENSE
- 示例程序
- 运行结果
SHIFT-字符串移位 和 CONDENSE去除空格
SHIFT BY n PLACES RIGHT/LEFT
*&---------------------------------------------------------------------*
*& Report Z_TEST_SHIFT_LHY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_test_shift_lhy.DATA: lv_str1 TYPE string,lv_str2 TYPE string,lv_str3 TYPE string.lv_str1 = 'ABC123'.
lv_str2 = 'DEF456'.
lv_str3 = 'GHI789'.
WRITE:/'lv_str1:',lv_str1.
SHIFT lv_str1. "向左移动一位WRITE:/'lv_str1向左移动一位:',lv_str1.
SKIP 1.WRITE:/'lv_str2:',lv_str2. "输出BC123
SHIFT lv_str2 BY 2 PLACES RIGHT. "向右移动两位WRITE:/'lv_str2 向右移动两位:',lv_str2. "输出: DEF456
SKIP 1.WRITE:/'lv_str3:',lv_str3.
SHIFT lv_str3 by 3 PLACES LEFT. "向左移动三位
WRITE:/'lv_str3 向左移动三位:',lv_str3. "输出789
运行结果
SHIFT … UP TO …
"SHIFT c UP TO c1. 表示指定字符串c从子串c1进行位移,执行结果返回系统参数SY-SUBRC,执行成功返回0,不匹配则返回4
SHIFT lv_str4 UP TO lv_sub1.
WRITE:/'lv_str4 = ',lv_str4.
WRITE:/'SY-SUBRC',sy-subrc. "输出0SHIFT lv_str5 UP TO lv_sub2.
WRITE:/'lv_str5 = ',lv_str5.
WRITE:/'SY-SUBRC',sy-subrc. "输出4
运行结果
第一个已经移动成功,第二个没有移动成功:
其他的-变量后面加括号和数字
SHIFT c LEFT/RIGHT DELETING
-
移除字符串c左边的子字符串c1
SHIFT c LEFT DELETING LEADING c1. -
移除字符串c右边的子字符串c1
SHIFT c RIGHT DELETING TRAILING c1.
DATA: lv_s1 TYPE string,lv_s2 TYPE string,lv_su1(3) TYPE c VALUE 'ABC',lv_su2(3) TYPE c VALUE 'EFG'.
lv_s1 = 'ABCDEFG'.
SHIFT lv_s1 BY 3 PLACES RIGHT.
WRITE:/'lv_s1:' ,lv_s1."删除空格
SHIFT lv_s1 LEFT DELETING LEADING space.
WRITE:/'lv_s1:',lv_s1."删除前面的ABC
SHIFT lv_s1 LEFT DELETING LEADING lv_su1.
WRITE:/'lv_s1:',lv_s1.SHIFT lv_s1 RIGHT DELETING TRAILING lv_su2.
WRITE:/'lv_s1:',lv_s1.
运行结果
向右移动的时候,左侧会用空格补位
SHIFT 去除0
示例程序1
DATA: lv_s1 TYPE string,lv_s2 TYPE string,lv_su1(3) TYPE c VALUE 'ABC',lv_su2(3) TYPE c VALUE 'EFG'.
lv_s2 = '000000012345'.
WRITE:/'原来的lv_s2:' ,lv_s2.
SHIFT lv_s2 LEFT DELETING LEADING '0'.WRITE:/'lv_s2删除前面的0之后:' ,lv_s2.
运行结果
示例程序2
DATA: lv_s1 TYPE string,lv_s2 TYPE string,lv_su1(3) TYPE c VALUE 'ABC',lv_su2(3) TYPE c VALUE 'EFG'.
lv_s1 = 'ABCDEFG'.
lv_s2 = '1234500000'.WRITE:/'原来的lv_s2:' ,lv_s2.
"SHIFT lv_s2 LEFT DELETING LEADING '0'."WRITE:/'lv_s2删除前面的0之后:' ,lv_s2.
SHIFT lv_s2 RIGHT DELETING TRAILING '0'.
WRITE :/'lv_s2 删除后面的0之后:' ,lv_s2.
运行结果
如果是向右移动,去掉没用的小数位的0,向右移动,看不到0为止,左边会填充空格
CONDENSE
CONDENSE是可以去除空格的
示例程序
DATA: lv_s1 TYPE string,lv_s2 TYPE string,lv_su1(3) TYPE c VALUE 'ABC',lv_su2(3) TYPE c VALUE 'EFG'.
lv_s1 = 'ABCDEFG'.
lv_s2 = '1234500000'.
WRITE:/'原来的lv_s2:' ,lv_s2.
"SHIFT lv_s2 LEFT DELETING LEADING '0'."WRITE:/'lv_s2删除前面的0之后:' ,lv_s2.
SHIFT lv_s2 RIGHT DELETING TRAILING '0'.
WRITE :/'lv_s2 删除后面的0之后:' ,lv_s2.
CONDENSE lv_s2.
WRITE :/'lv_s2 删除空格之后:' ,lv_s2.
运行结果
在删除之前是: 12345