ABAP常用字符串操作

字符串首字符索引为 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 not
CA / NA contains any or not any
CS / 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'.

============================================================
============================================================
ABAP對字串的操作方法與其他語言的操作有較大差別,以下是較常用的對字串操作的方法:

1. 字串的連接:CONCATENATE
DATA: t1 TYPE c LENGTH 10 VALUE 'We',
t2 TYPE c LENGTH 10 VALUE 'have',
t3 TYPE c LENGTH 10 VALUE 'all',
t4 TYPE c LENGTH 10 VALUE 'the',
t5 TYPE c LENGTH 10 VALUE 'time',
t6 TYPE c LENGTH 10 VALUE 'in',
t7 TYPE c LENGTH 10 VALUE 'the',
t8 TYPE c LENGTH 10 VALUE 'world',
result TYPE string.

CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 INTO result. "直接連接
CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 INTO result SEPARATED BY space. "用空格將每個字串連接


2. 拆分字串: SPLIT
DATA: str1 TYPE string,
str2 TYPE string,
str3 TYPE string,
itab TYPE TABLE OF string,
text TYPE string.

text = `What a drag it is getting old`.
SPLIT text AT space INTO: str1 str2 str3,
TABLE itab.

3. 去除多余的空格:CONDENSE
DATA: BEGIN OF sentence,
word1 TYPE c LENGTH 30 VALUE 'She',
word2 TYPE c LENGTH 30 VALUE 'feeds',
word3 TYPE c LENGTH 30 VALUE 'you',
word4 TYPE c LENGTH 30 VALUE 'tea',
word5 TYPE c LENGTH 30 VALUE 'and',
word6 TYPE c LENGTH 30 VALUE 'oranges',
END OF sentence,
text TYPE string.

text = sentence.
CONDENSE text.“在每個連接串中間會有一個空格
CONDENSE text No-GAPS. "在後面加入NO-GAPS後,所有空格都會去除。

4.字串轉換:TRANSLATE
DATA text TYPE string.
text = `Careful with that Axe, Eugene`.
TRANSLATE text TO UPPER CASE. "轉大寫
TRANSLATE text TO LOWER CASE. "轉小寫

  DATA text TYPE string.
text = `Barbcbdbarb`.
TRANSLATE text USING 'ABBAabba'. “A與B換轉 a與b換轉

5.取字串長度:STRLEN
data text type c length 20.
data: ilen type i.
text = `Barbcbdbarb`.
ilen = strlen( text ). "字串實際長度(11)
describe field text length ilen in byte mode. "字串定義時的字節長度,一個長度等2個字節(40)
describe field text length ilen in character mode. "字串定義的長度(20)

6.截取子字串
data: text type c length 10.
data: ilen type i.
data: subtext type c length 5.
text = 'ABCDEFGHIJ'.
subtext = text+0(5). "取左邊5個字符('ABCDE')
subtext = text+3(5). "從第四個字串起取5個字符('DEFGH)
text+0(1) = 'Z'. "將第一個字符變爲'Z'
注意:+兩邊不能有空格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值