ABAP开发之字符串操作

1.字符串连接--CONCATENATE

CONCATENATE field1 field2 field3 INTO field [SEPERATED BY SPACE] [RESPECTING BLANKS].

2.字符串分割--SPLIT

SPLIT field AT '-' INTO field1 field2 field3.

3.字符串截取--使用加号(+)

field = field(n).截取前n个字符
field = field+n(m).从n处截取m个字符

4.字符串替换--REPLACE

REPLACE ALL OCCURRENCES OF pattern1 IN field WITH pattern2.

5.字符串大小写转换--TRANSLATE

TRANSLATE field TO UPPERCASE.
TRANSLATE field TO LOWERCASE.

6.字符串查找--FIND

FIND pattern IN field.  通过SY-SUBRC 进行判断

下面给出一段实例代码:

DATA:str1(10) TYPE C,
      str2(10) TYPE C,
      str3(10) TYPE C,
      str4(10) TYPE C,
      str5(20) TYPE C,
      str6(10) TYPE C,
      str7(10) TYPE C,
      str8(10) TYPE C,
      str9(10) TYPE C.
str1 = 'Hello '.
str2 = 'ABAP'.
*合并
WRITE:/ '合并:'.
CONCATENATE str1 str2 INTO str3."合并时候去掉空格
CONCATENATE str1 str2 INTO str4 SEPARATED BY  ','."合并时候加上指定字符
CONCATENATE str1 str2 INTO str5 RESPECTING BLANKS."合并时候保留前后空格
WRITE: / 'str3 =',str3,
       /'str4 =', str4,
       /'str5 =',str5.
*分割
WRITE:/ '分割:'.
SPLIT str4 AT ',' INTO str6 str7.
WRITE: / 'str6 =',str6,
       / 'str7 =',str7.


*截取
WRITE:/ '截取:'.
str8 = str3(6)."截取前6个字符
str9 = str3+2(6)."从第2位字符开始,截取6个字符
WRITE: / 'str8 =',str8,
       / 'str9 =',str9.
*替换
WRITE:/ '替换:'.
REPLACE ALL OCCURRENCES  OF 'll' IN str3 WITH 'oo'.
WRITE:/ 'str3 =',str3.
*大小写转换
WRITE:/ '大小写转换:'.
TRANSLATE str3 TO UPPER CASE.
WRITE:/ str3.
TRANSLATE str3 TO LOWER CASE.
WRITE:/ str3.
*搜索指定字符串
WRITE:/ '搜索指定字符串:'.
SEARCH str3 FOR 'e'.
IF SY-SUBRC = 0.
  WRITE:/ '指定字符串第一次出现的位置:',SY-FDPOS."查找内容第一次出现的位置
ENDIF.

执行结果:

ABAP开发之字符串操作_第1张图片

你可能感兴趣的:(ABAP开发之字符串操作)