ABAP 字符串函数

时间:2024-10-05 18:06:02

CONCATENATE:合并字符串.

CONCATENATE f1 … fn INTO g [SEPARATED BY h].

ABAP 字符串函数
 1 * CONCATENATE合并字符串
2 DATA: c1(10) TYPE c VALUE 'Sum',
3 c2(3) TYPE c VALUE 'mer',
4 c3(5) TYPE c VALUE 'holi ',
5 c4(10) TYPE c VALUE 'day',
6 c5(30) TYPE c ,
7 sep(3) TYPE c VALUE ' - '.
8 CONCATENATE c1 c2 c3 c4 INTO c5.
9 WRITE c5.
10 CONCATENATE c1 c2 c3 c4 INTO c5 SEPARATED BY sep.
11 WRITE / c5.
ABAP 字符串函数

--------------------------------------------------------

输出结果:
Summerholiday
Sum - mer - holi - day

--------------------------------------------------------

SPLIT: 字符串拆分.

SPLIT f AT g INTO h1 … hn.

SPLIT f AT g INTO TABLE itable.

ABAP 字符串函数
 1 * splitting strings
2 DATA: string10(60) TYPE c ,
3 p1(20) TYPE c VALUE '++++++++++++++++++++',
4 p2(20) TYPE c VALUE '++++++++++++++++++++',
5 p3(20) TYPE c VALUE '++++++++++++++++++++',
6 p4(20) TYPE c VALUE '++++++++++++++++++++',
7 del10(3) TYPE c VALUE '***'.
8 string10 = ' Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5'.
9 WRITE string10.
10 SPLIT string10 AT del10 INTO p1 p2 p3 p4.
11 WRITE / p1.
12 WRITE / p2.
13 WRITE / p3.
14 WRITE / p4.
ABAP 字符串函数

--------------------------------------------------------

输出结果:

Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5

Part 1

Part 2

Part 3

Part 4 *** Part 5

--------------------------------------------------------

SHIFT:字符串整体或者字串进行转移.

如果SHIFT 操作的对象是 C类型, 则所有字节都会向前移动一位,最后一位用空格代替;

如果SHIFT操作的对象是String类型,则所有字符都会向前移动一位,最后一位删除;

SHIFT c <LEFT/RIGHT/CIRCULAR>.

SHIFT c BY n PLACES.

SHIFT c UP TO c1.

ABAP 字符串函数
 1 * SHIFT c BY n PLACES 用法.
2 DATA: t1(10) TYPE c VALUE 'abcdefghij',
3 string1 LIKE t1.
4
5 string1 = t1.
6 WRITE string1.
7 SHIFT string1.
8 WRITE / string1.
9 string1 = t1.
10 SHIFT string1 BY 3 PLACES LEFT.
11 WRITE / string1.
12 string1 = t1.
13 SHIFT string1 BY 3 PLACES RIGHT.
14 WRITE / string1.
15 string1 = t1.
16 SHIFT string1 BY 3 PLACES CIRCULAR.
17 WRITE / string1.
ABAP 字符串函数

--------------------------------------------------------

输出结果:

abcdefghij   “string1

bcdefghij

defghij

abcdefg

defghijabc

--------------------------------------------------------

ABAP 字符串函数
 1 * SHIFT c UP TO c1
2 DATA: t2(10) TYPE c VALUE 'abcdefghij',
3 string2 LIKE t2,
4 str2(2) TYPE c VALUE 'ef'.
5
6 string2 = t2.
7 WRITE string2.
8 SHIFT string2 UP TO str2.
9 WRITE / string2.
10 string2 = t2.
11 SHIFT string2 UP TO str2 LEFT.
12 WRITE / string2.
13 string2 = t2.
14 SHIFT string2 UP TO str2 RIGHT.
15 WRITE / string2.
16 string2 = t2.
17 SHIFT string2 UP TO str2 CIRCULAR.
18 WRITE / string2.
ABAP 字符串函数

--------------------------------------------------------

输出结果:

abcdefghij

efghij

efghij

abcdef

efghijabcd

--------------------------------------------------------

移除字符串左/右边的子字符串:

SHIFT c LEFT DELETEING LEADING c1.

SHIFT c RIGHT DELETEING TRAILING c1.

ABAP 字符串函数
 1 * SHIFT c LEFT/RIGHT DELETEING LEADING c1
2 DATA: t3(14) TYPE c VALUE ' abcdefghij',
3 string3 LIKE t3,
4 str3(6) TYPE c VALUE 'ghijkl'.
5
6 string3 = t3.
7 WRITE string3.
8 SHIFT string3 LEFT DELETING LEADING space.
9 WRITE / string3.
10 string3 = t3.
11 SHIFT string3 RIGHT DELETING TRAILING str3.
12 WRITE / string3.
ABAP 字符串函数

--------------------------------------------------------

输出结果:

abcdefghij

abcdefghij

abcdef

--------------------------------------------------------

CONDENSE:重新整合分配字符串.

CONDENSE c <NO-GAPS>.

ABAP 字符串函数
 1 * condensing strings
2 DATA: string9(25) TYPE c VALUE ' one two three four',
3 len9 TYPE i.
4
5 len9 = strlen( string9 ).
6 WRITE: string9, '!'.
7 WRITE: / 'Length: ', len9.
8 CONDENSE string9.
9 len9 = strlen( string9 ).
10 WRITE: string9, '!'.
11 WRITE: / 'Length: ', len9.
12 CONDENSE string9 NO-GAPS.
13 len9 = strlen( string9 ).
14 WRITE: string9, '!'.
15 WRITE: / 'Length: ', len9.
ABAP 字符串函数

--------------------------------------------------------

输出结果:

one  two   three    four !

Length:          25  one two three four        !

Length:          18  onetwothreefour           !

Length:          15

--------------------------------------------------------

TRANSLATE:实现字符串转换.

TRANSLATE c TO UPPER CASE.

TRANSLATE c TO LOWER CASE.

TRANSLATE c USING c1.

ABAP 字符串函数
 1 * translating signs
2 DATA: t5(10) TYPE c VALUE 'AbCdEfGhIj',
3 string5 LIKE t5,
4 rule5(20) TYPE c VALUE 'AxbXCydYEzfZ'.
5
6 string5 = t5.
7 WRITE string5.
8 TRANSLATE string5 TO UPPER CASE. "#EC SYNTCHAR
9 WRITE / string5.
10 string5 = t5.
11 TRANSLATE string5 TO LOWER CASE. "#EC SYNTCHAR
12 WRITE / string5.
13 string5 = t5.
14 TRANSLATE string5 USING rule5. "#EC SYNTCHAR
15 WRITE / string5.
ABAP 字符串函数

----------------------------------------------

输出结果:

AbCdEfGhIj

ABCDEFGHIJ

abcdefghij

xXyYzZGhIj

----------------------------------------------

TRANSLATE c … FROM CODE PAGE g1 … TO CODE PAGE g2.

OVERLAY:参考字符串对空白字符进行填充.

OVERLAY c with c1.

REPLACE:字符串按条件取代.

REPLACE f … WITH g … INTO h.

ABAP 字符串函数
 1 * replacing values
2 DATA: t4(10) TYPE c VALUE 'abcdefghij',
3 string4 LIKE t4,
4 str41(4) TYPE c VALUE 'cdef',
5 str42(4) TYPE c VALUE 'klmn',
6 str43(2) TYPE c VALUE 'kl',
7 str44(6) TYPE c VALUE 'klmnop',
8 len4 TYPE i VALUE 2.
9
10 string4 = t4.
11 WRITE string4.
12 REPLACE str41 WITH str42 INTO string4.
13 WRITE / string4.
14 string4 = t4.
15 REPLACE str41 WITH str42 INTO string4 LENGTH len4.
16 WRITE / string4.
17 string4 = t4.
18 REPLACE str41 WITH str43 INTO string4.
19 WRITE / string4.
20 string4 = t4.
21 REPLACE str41 WITH str44 INTO string4.
22 WRITE / string4.
ABAP 字符串函数

----------------------------------------------

输出结果:

abcdefghij

abklmnghij

abklmnefgh

abklghij

abklmnopgh

--------------------------------------------------------

SERACH:搜索指定字符串. 通过SY_SUBRC取值得到查询结果.

SERACH f FOR g.

[ABBREVIATED] : 从指定字符串中按顺序搜索相匹配字符串 abbreviated

[STARTING AT n1 ]:从字符串第n1个字符串开始搜索

[ENDING AT n2 ]:搜索到字符串第 n2个字符为止

[AND MARK]:从指定字符串中模糊搜索相匹配字符串

ABAP 字符串函数
 1 *searching strings
2 DATA string7(30) TYPE c VALUE 'This is a little sentence.'.
3 WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.
4
5 ULINE /1(26).
6 SEARCH string7 FOR 'X'.
7 WRITE: / 'X', sy-subrc UNDER 'SY-SUBRC',
8 sy-fdpos UNDER 'SY-FDPOS'.
9 SEARCH string7 FOR 'itt '.
10 WRITE: / 'itt ', sy-subrc UNDER 'SY-SUBRC',
11 sy-fdpos UNDER 'SY-FDPOS'.
12 SEARCH string7 FOR '.e .'.
13 WRITE: / '.e .', sy-subrc UNDER 'SY-SUBRC',
14 sy-fdpos UNDER 'SY-FDPOS'.
15 SEARCH string7 FOR '*e'.
16 WRITE: / '*e ', sy-subrc UNDER 'SY-SUBRC',
17 sy-fdpos UNDER 'SY-FDPOS'.
18 SEARCH string7 FOR 's*'.
19 WRITE: / 's* ', sy-subrc UNDER 'SY-SUBRC',
20 sy-fdpos UNDER 'SY-FDPOS'.
ABAP 字符串函数

----------------------------------------------

输出结果:

Searched SY-SUBRC SY-FDPOS

--------------------------------------------

X            4        0

itt           0       11

.e .          0       15

*e           0       10

s*           0       17

----------------------------------------------

ABAP 字符串函数
 1 *
2 DATA: string8(30) TYPE c VALUE 'This is a fast first example.',
3 pos8 TYPE i,
4 off8 TYPE i.
5
6 WRITE / string8.
7 SEARCH string8 FOR 'ft' ABBREVIATED.
8 WRITE: / 'SY-FDPOS:', sy-fdpos.
9 pos8 = sy-fdpos + 2.
10 SEARCH string8 FOR 'ft' ABBREVIATED STARTING AT pos8 AND MARK.
11 WRITE / string8.
12 WRITE: / 'SY-FDPOS:', sy-fdpos.
13 off8 = pos8 + sy-fdpos - 1.
14 WRITE: / 'Off:', off8.
ABAP 字符串函数

----------------------------------------------

输出结果:

This is a fast first example.

SY-FDPOS:    10

This is a fast FIRST example.

SY-FDPOS:     4

Off:         15

----------------------------------------------

SERACH itab FOR g.

[ABBREVIATED] : 从内表中按顺序逐行搜索相匹配字符串

[STARTING AT line1 ]:从字符串第n1个字符串开始搜索

[ENDING AT line2 ]:搜索最大范围到内表中具体某行

[AND MARK]:从内表中模糊搜索相匹配字符串

搜索内表与字符串操作一致:

PS:其中源码部分来自SAP官方文档。