abap 字串操作 ABAP 字串常用處理方法

2021-10-16 19:35:14 字數 4648 閱讀 2196

split dobj at sep into | }

必須指定足夠目標字段。否則,用字段dobj的剩餘部分填充最後目標欄位幷包含分界符;或者使用內錶動態接收

data: l_str type string,

l_str1 type c,

l_str2 type c.

data: begin of lt_str occurs 0,

str type c,

end of lt_str.

l_str = '1/2'.

split l_str at '/' into l_str1 l_str2. "根據『/』截斷字元創 l_str 放於l_str1、l_str2

write: l_str1,l_str2,/.

split l_str at '/' into table lt_str. "根據『/』截斷字元創 l_str 放於表lt_str

loop at lt_str.

write: lt_str-str,/.

endloop.

shift dobj | ] [ [ left|right ] [ circular ] ] }

​| | } pattern

對於固定長度字串型別,shift產生的空位會使用空格或十六進製制的0(如果為x型別串時)來填充

向右移動時前面會補空格,固定長度型別字串與string結果是不一樣:string型別右移後不會被截斷,只是字串前面補相應數量的空格,但如果是c型別時,則會截斷;左移後後面是否被空格要看是否是固定長度型別的字串還是變長的string型別串,左移後c型別會補空格,string型別串不會(會縮短)

circular:將移出的字串放在左邊或者左邊

pattern:只要前導或尾部字元在指定的pattern字符集裡就會被去掉,直到第乙個不在模式pattern的字元止

data(str) = `0123456789`.

shift str."列印出 123456789

data: text type string value 'i know you know',

off type i.

find 'you' in text match offset off.

shift text by off places."從 『you』 的地方截斷字串text,保留'you' 和之後的字串 you know

*shift text up to 'you'."從 『you』 的地方截斷字串text,保留'you' 和之後的字串 you know

data(text) = '0123456789'.

*shift text right."從右邊截斷第乙個字元012345678

shift text left."從左邊截斷第乙個字元123456789

shift text up to 'you' left circular."列印出來是 『you know i know』

shift text up to 'you' right circular. "列印出來是 『know i know you』

data(xstr) = conv xstring( `aabbccddeeff` ).

shift xstr in byte mode. "列印出來 bbccddeeff

data text type string value `i know you know `.

shift text right deleting trailing 'no kw'. "i know you

data txt type string value '0000011111'.

shift txt right deleting trailing '1'. "00000

shift txt left deleting leading '0'. "11111

condense [no-gaps].

如果是c型別只去掉前面的空格(因為是定長,即使後面空格去掉了,左對齊時後面會補上空格),如果是string型別,則後面空格也會被去掉;

字串中間的多個連續的空格使用乙個空格替換(string型別也是這樣);

no-gaps:字串中間的所有空格都也都會去除(string型別也是這樣);空格去掉後會左對齊

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." she feeds you tea and oranges

condense text no-gaps."shefeedsyouteaandoranges

concatenate | into result [separated by sep] [respecting blanks].

c、d、n、t型別的前導空格會保留,尾部空格都會被去掉,但對string型別所有空格都會保留;對於c、d、n、t型別的字串有乙個respecting blanks選項可使用,表示尾部空格也會保留。注:使用 `` 對string型別進行賦值時才會保留尾部空格

types hex type x length 1.

data itab type table of hex with empty key.

itab = value #(

( conv hex( '48' ) )

( conv hex( '65' ) )

( conv hex( '6c' ) )

( conv hex( '6c' ) )

( conv hex( '6f' ) ) ).

concatenate lines of itab into data(xstr) in byte mode."48656c6c6f

strlen(arg)、xstrlen(arg)

string型別的尾部空格會計入字元個數中,但c型別的變數尾部空格不會計算入

data: str type string value `12345 `,

txt type c length 10 value '12345 ',

len type i.

len = strlen( str )."10

len = strlen( txt )."5

substring( val = text [off = off] [len = len] )

data result type string.

result = substring( val = 'abcdefgh' off = 2 len = 2 )."cd

result = substring_from( val = 'abcdefgh' sub = 'cd' )."cdefgh

result = substring_after( val = 'abcdefgh' sub = 'cd' )."efgh

result = substring_before( val = 'abcdefgh' sub = 'cd' )."ab

result = substring_to( val = 'abcdefgh' sub = 'cd' )."abcd

count( val = text | )

匹配指定字串substring或正則式regex出現的子串次數,返回的型別為i整型型別

data(result1) = count( val = `***123yyy` regex = `\d+` )."1

data(result2) = count_any_of( val = `***123yyy` sub = `123` )."3

data(result3) = count_any_not_of( val = `***123yyy` sub = `x` )."6

match( val = text regex = regex occ = occ)

返回的為匹配到的字串。注:每次只匹配乙個。

occ:表示需匹配到第幾次出現的子串。如果為正,則從頭往後開始計算,如果為負,則從尾部向前計算

match( val = 'x1 x2 x3' regex = 'x.' occ = 2 )"x2

find( val = text |[occ = occ] )

查詢substring或者匹配regex的子串的位置。如果未找到,則返回 -1,返回的為offset,所以從0開始

data(result1) = find( val = `***123yyy` regex = `\d+` )."3

data(result2) = find_end( val = `***123yyy` regex = `\d+` )."6

data(result3) = find_any_of( val = `***123yyy` sub = `123` )."3

data(result4) = find_any_not_of( val = `***123yyy` sub = `x` )."3

abap 字串處理

1 對字串的操作 1 shift 截斷字串 shift by places 作用 去掉字串的前n個位置的字元,如果n未指定,預設為1,如果指定的n小於等於0,則字串不變。如果n超出字串的長度,則字串變空,所以在做此操作的時候要注意n的指定。可以首先獲得該字串的長度,方法 len strlen c m...

abap字串控制函式

字串控制函式 concatenate 實現字串合併 語法 concatenate f1.fn into g by h data one 10 value hello two 10 value sap result1 10 result2 10 concatenate one two into res...

ABAP 擷取字串(Start Routine)

需求 有主資料zplant,取自工廠描述的一部分,用 隔開,比如 工廠a 北京 zplant只需要 工廠a 怎麼解決呢 寫在start routine裡,data z res wa typematch result,z res tab typematch result tab,z data type...