ABAP-字符串常⽤处理⽅法
字符串处理
1. SPLIT dobj AT sep INTO { {result1 result2 ...} | {TABLE result_tab} }
必须指定⾜够⽬标字段。否则,⽤字段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.
2. SHIFT dobj { [ {BY num PLACES} | { UP TO sub_string } ] [ [ LEFT|RIGHT ] [ CIRCULAR ] ] }
| { { LEFT DELETING LEADING } | { RIGHT DELETING LEADING } } 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
3. 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
4. CONCATENATE { dobj1 dobj2 ...} | { LINES OF itab } 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
5. 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
6. substring( val = TEXT [off = off] [len = len] )
c++string类型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
7. count( val = TEXT {sub = substring}|{regex = regex} )
匹配指定字符串substring或正则式regex出现的⼦串次数,返回的类型为i整型类型
DATA(RESULT1) = COUNT( VAL = `xxx123yyy` REGEX = `\d+` ). "1
DATA(RESULT2) = COUNT_ANY_OF( VAL = `xxx123yyy` SUB = `123` ). "3
DATA(RESULT3) = COUNT_ANY_NOT_OF( VAL = `xxx123yyy` SUB = `x` ). "6
8. match( val = TEXT REGEX = REGEX occ = occ)
返回的为匹配到的字符串。注:每次只匹配⼀个。
occ:表⽰需匹配到第⼏次出现的⼦串。如果为正,则从头往后开始计算,如果为负,则从尾部向前计算
MATCH( VAL = 'x1 x2 x3' REGEX = 'x.' OCC = 2 ) "x2
9. find( val = TEXT {sub = substring}|{regex = regex}[occ = occ] )
查substring或者匹配regex的⼦串的位置。如果未到,则返回 -1,返回的为offset,所以从0开始
DATA(RESULT1) = FIND( VAL = `xxx123yyy` REGEX = `\d+` ). "3
DATA(RESULT2) = FIND_END( VAL = `xxx123yyy` REGEX = `\d+` ). "6
DATA(RESULT3) = FIND_ANY_OF( VAL = `xxx123yyy` SUB = `123` ). "3
DATA(RESULT4) = FIND_ANY_NOT_OF( VAL = `xxx123yyy` SUB = `x` ). "3
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论