本人关于学习ABAP的一些笔记,分享出来,对于初学者应该会有一点帮助,大神请跳过,后续会更新多个文章,欢迎指正和留言沟通!
基本语法
- 定义数据和类型
REPORT ZWANGZHENG2.
*定义数据txt1 类型 C 长度15 初始值 HELLO WORLD
DATA txt1(15) TYPE c VALUE 'HELLO WORLD'.
*定义类型t_txt
TYPES t_txt(15) TYPE c .
*定义数据txt2 类型为:t_txt
DATA txt2 TYPE t_txt VALUE 'first'.
*定义类型为N的数据及类型。
DATA i1(2) TYPE n VALUE 118.
TYPES my_number_type(5) TYPE n.
DATA i2 TYPE my_number_type.
*定义时间类型数据
Data time1 TYPE t VALUE '100701'.
WRITE time1.
*定义日期类型数据
DATA day1 TYPE d.
day1 ='20110628'.
DATA i1 TYPE I.
i1 = 15.
WRITE i1.
*定义结构体
TYPES:
BEGIN OF student,
studentid type I,
sname(20) type c,
sex(10) type c,
age type I,
END OF student.
DATA stu1 TYPE student.
stu1-studentid = 1001 .
stu1-sname = 'tom'.
stu1-sex = 'man'.
stu1-age = 18.
WRITE stu1-studentid .
WRITE stu1-sname .
WRITE stu1-sex .
WRITE stu1-age .
运算符 控制流语句
REPORT ZWANGZHENG6.
DATA a TYPE i VALUE 1.
DATA b TYPE i VALUE 20.
DATA c TYPE i .
a的 b次方.
c = a ** b .
* b = a + b.
ADD a TO b.
- case语句
CASE a.
when 12 OR 1 OR 2 .
WRITE / 'winner' .
when 3 OR 4 OR 5 .
WRITE / 'spring' .
when 6 OR 7 OR 8 .
WRITE / 'summer' .
when 9 OR 10 OR 11 .
WRITE / 'winner' .
when others.
WRITE / 'NO'.
ENDCASE .
- 固定次数循环
*输出1-30之间偶数 - sy-index 循环次数 从1开始
DATA a TYPE i .
DO 30 TIMES.
a = sy-index.
DATA s TYPE i .
s = a MOD 2 .
IF s = 0 .
WRITE / sy-index .
ENDIF .
ENDDO.
DATA sum1 TYPE i value 0 .
DATA sum2 TYPE i value 0 .
*循环 计算1~100之间的奇数和 偶数和。
WHILE sy-index <= 100 .
a = sy-index .
b = a mod 2 .
if b = 0 .
sum1 = sum1 + a .
else .
sum2 = sum2 + a .
ENDIF .
ENDWHILE.
WRITE / sum1 .
WRITE / sum2 .
循环控制
CONTINUE :结束本次循环
EXIT跳出整个循环 相当于BREAK
字符串操作
字符串连接
REPORT ZWANGZHENG7.
DATA:
s1(20) TYPE c value 'aaa',
s2(20) TYPE c value 'bbb' ,
s3(20) TYPE c ,
sep(1) TYPE c value '.' .
- s3=s1+s2.中间无空格
CONCATENATE s1 s2 INTO s3 .
WRITE / s3 .
- s3=s1+s2 连接处使用数据sep中的值填充
CONCATENATE s1 s2 INTO s3 SEPARATED BY sep .
WRITE / s3 .
字符串拆分
REPORT ZWANGZHENG8.
DATA: t Type string,
i TYPE TABLE OF string,
sep TYPE c value '*'.
t = 'ABAP*is*a*programming*language' .
*拆分字符串,把 t 按照 sep 分隔符 拆分 拆分结果放入到 i 表中。
SPLIT t At sep INTO TABLE i .
*迭代 内表 i 每个元素,将元素 一个个付给 t
LOOP AT i INTO t.
WRITE / t .
END
字符串查找
REPORT ZWANGZHENG9.
DATA s(30) TYPE c VALUE 'This is a testing sentence.' .
WRITE: / 'Searched' , 'SY-SUBRC' , 'SY-FDPOS'.
SEARCH s for 'x' .
WRITE :/ 'x' , sy-subrc UNDER 'SY-SUBRC'
, SY-fdpos UNDER 'SY-FDPOS' .
SEARCH s for 'ing ' .
WRITE :/ 'ing ' , sy-subrc UNDER 'SY-SUBRC'
, sy-fdpos UNDER 'SY-FDPOS' .
SEARCH s for '.ing ' .
WRITE :/ '.ing ' , sy-subrc UNDER 'SY-SUBRC'
, sy-fdpos UNDER 'SY-FDPOS' .
SEARCH s for 's*' .
WRITE :/ 's*' , sy-subrc UNDER 'SY-SUBRC'
, sy-fdpos UNDER 'SY-FDPOS' .
SEARCH s for '*s' .
WRITE :/ '*s' , sy-subrc UNDER 'SY-SUBRC'
, sy-fdpos UNDER 'SY-FDPOS' .
字符串替换
REPORT zwangzheng10.
DATA s TYPE string.
s = 'aaa-ss-dd-fff-ggg-sss'.
WRITE sy-subrc ."sy-subrc初始值为0
WHILE sy-subrc = 0 .
REPLACE '-' WITH ' ' INTO s .
ENDWHILE .
WRITE s .
字符串长度
REPORT zwangzheng11.
DATA: s TYPE string VALUE '123456789' ,
len TYPE i .
len = STRLEN( s ).
WRITE len .
定位操作字符串
REPORT ZWANGZHENG12.
DATA: s1(8) TYPE c VALUE 'abcdefgh' ,
s2(20) TYPE c value '1234567890' .
*将 s2 从下标为2的位子开始 往后3个字符串,
*替换成s1从下标为2的位子开始 往后3个字符串
s2+2(3) = s1+2(3) .
WRITE s1 .
WRITE s2 .
子程序
无参数
data: a type i value 1 ,
b type i value 2,
sum type i.
perform add.
write sum.
form add.
sum = a + b.
endform.
带参数
DATA: a TYPE i ,
b TYPE i ,
c TYPE i ,
d TYPE i ,
sum TYPE i.
START-OF-SELECTION.
a = 1.
b = 2.
PERFORM add USING a b.
WRITE sum.
FORM add USING c d.
sum = c + d.
WRITE sum.
ENDFORM.
参数传值和传地址对比
DATA: a TYPE i ,
b TYPE i .
START-OF-SELECTION.
a = 1. b = 1.
"a传地址 b传值
PERFORM add USING a b.
write : / a , b.
*输出结果 2 1
FORM add USING c type i value(d) type i.
c = c + 1.
d = d + 1.
ENDFORM.
以上仅仅是我作为一个出学者的理解,以及一些尚未成熟的记录!欢迎留言指正。
谢谢!待续。。。