Wednesday, October 26, 2011

ABAP常用字符串操作

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' 注意:+兩邊不能有空格。

No comments: