【SAP ABAP系列】ABAP常用字符串操作收集整理

2020-11-06 10:22:39 浏览数 (1)

字符串首字符索引为 0; Character Fields: C,N, D, T, string (CNDT=> CN Data Time)

1. 字符串连接

CONCATENATE dobj1 dobj2 ... INTO result

[IN { BYTE | CHARACTER } MODE]

[SEPARATED BY sep].

2.字符串分隔, split 一个string的部分到一个内表或一系列的变量

SPLIT dobj AT sep INTO

{ {result1 result2 ...} | {TABLE result_tab} }

[IN {BYTE|CHARACTER} MODE].

3. 字符串查找, 在一个字符串中查找模式串(FIND or SEARCH)

FIND sub_string

IN SECTION [OFFSET off] [LENGTH len] OF dobj --> 灰色部分用来缩小目的串被查找的范围

[ IN { BYTE | CHARACTER } MODE ]

[ { RESPECTING | IGNORING } CASE ]

[ MATCH OFFSET moff ] [MATCH LENGTH mlen ].

FIND 'knows'

IN SECTION OFFSET 5 OF 'Everybody knows this is nowhere'

MATCH OFFSET moff " => moff = 10

MATCH LENGTH mlen. " => mlen= 5

在字符串dobj中查找pattern

SEARCH dobj FOR pattern [IN { BYTE | CHARACTER } MODE]

[STARTING AT p1] [ENDING AT p2]

[ABBREVIATED]

[AND MARK].

if sy-subrc = 0. then SY-FDPOS = 返回pattern在dobj中的位置

About pattern:

'pat' - 忽略尾部空格

'.pat.' -不忽略尾部空格

'*pat' - 以pat结尾

'pat*' - 以pat开始

单词是指: 用 空格 , ; : ? ! () / =分隔的字串

4. 字符串替换

REPLACE SECTION [OFFSET off] [LENGTH len]

OF dobj WITH new

[IN { BYTE | CHARACTER } MODE].

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]

[SUBSTRING] sub_string

IN [SECTION [OFFSET off] [LENGTH len] OF ] dobj WITH new

[IN {BYTE|CHARACTER} MODE]

[{RESPECTING|IGNORING} CASE]

[REPLACEMENT COUNT rcnt]

[REPLACEMENT OFFSET roff]

[REPLACEMENT LENGTH rlen].

eg:

DATA: text1 TYPE string VALUE 'xababx'.

REPLACE 'ab' IN text1 WITH 'xx'. --> xxxabx

5. 去前导0 (Remove leading zero)

SHIFT dobj LEFT DELETING LEADING '0'.

FM : CONVERSION_EXIT_ALPHA_OUTPUT

增前导0 (Add leading zero)

DATA v_s(5).

UNPACK '123' to v_s. ==> v_s = '00123'

FM: CONVERSION_EXIT_ALPHA_INPUT

SHIFT dobj

[{BY num PLACES} | {UP TO sub_string}]

[LEFT|RIGHT] [CIRCULAR]

SHIFT dobj

{LEFT DELETING LEADING} | {RIGHT DELETING TRAILING} pattern.

[IN {BYTE|CHARACTER} MODE].

6. 字符串的长度, 内表的行数

STRLEN( dobj) 字符串的长度

LINES( itab ) 内表的行数

7. 删字符串中的空格: CONDENSE text [NO-GAPS].

8. 大小写转换,字符变换

TRANSLATE text {TO {UPPER|LOWER} CASE} | {USING pattern}.

eg: text = `Barbcbdbarb`.

TRANSLATE text USING 'ABBAabba'. =>'Abracadabra'

9. CONVERT

CONVERT DATE dat [TIME tim [DAYLIGHT SAVING TIME dst]]

INTO TIME STAMP time_stamp TIME ZONE tz.

CONVERT TIME STAMP time_stamp TIME ZONE tz

INTO [DATE dat] [TIME tim]

[DAYLIGHT SAVING TIME dst].

CONVERT TEXT text INTO SORTABLE CODE hex.

10. OVERLAY text1 WITH text2 [ONLY pattern].

如果不指定后面的ONLY pattern, text1中的空格会被text2中的对应字符替代

如果指定只有匹配的字符才会被替代,注意大小写敏感

11. 模式匹配

CO / CN contains only or notCA / NA contains any or not anyCS / NS contain string or not

CP / NP contains pattern or not

NOTE:

a) . CO, NO, CA, NA比较时区分大小写, 并且尾部空格也在比较的范围之内

data: s1(10) value 'aabb'.

if s1 co 'ab' ==> false

if s1 co 'ab ' ==>true

CS, NS, CP, NP不区分大小写和尾部空格

b) .对于CP, NP

* = s?

= s

# 换码字符, 用于匹配 *, 这样的字符

##

#*

#

#___ 比较结尾空格

#[a-z] 在CP, NP中强制区分大小写

c) . 比较结束后,如果结果为真,sy-fdpos将给出s2在s1中的偏移量信息

12. 特殊字符

在字符串中加入回车换行或TAB字符,在其他语言可以使用$13$10这样的ASCII码进行插入.但在ABAP中要使用sap的类CL_ABAP_CHAR_UTILITIES. 里面有字符常量:CR_LF,HORIZONTAL_TAB,NEWLINE等等.

13. 字符串位操作

DATA: v_s(10) value 'abcd'.

v_s 0(1) = 'b'.

v_s 2(*) = '12'.

=> v_s = 'bb12'.

0 人点赞