天天看点

abap 字符类型 和处理方法

字符类型:    一.包括:          C:字符          N:应该算是字符型的数字          D:日期类型,标准长度8(YYYYMMDD),注意其初期值的判断,不是space,而是'00000000',判断语句可以使用is initial or not。          T:时间类型,标准长度6(HHMMSS)  

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'. 

补零 数字前补零用途: 在查语句中,'00006' 和 '6' 是不同的字符,而SAP会自动将查询条件变量前面的零去掉。导致查询条件不正确,这时候要用CONVERSION_EXIT_ALPHA_INPUT进行补零。注意:变量类型要与数据库字段类型一直,否则补零的位数不正确。 ------------------------------------------------------------ 字符串长度 strlen( char_var ) ------------------------------------------------------------ 回车符 A Virtual Characterstic is a normal Characterstic,The Only difference is the data will be Updated at the time of Query Execution.Just Create a Characterstic add this Char to the Cube.You need to write some ABAP Code to Update this Char. You will find the Doumentation and Example in SMOD for Virtual Characterstics and Keyfigures. for Documentation and Example. Goto Tcode SMOD -> Enter Enhancement as RSR00002 ,Select the Radio button Documentation -> Click on Display 用sap的类CL_ABAP_CHAR_UTILITIES(  TYPE-POOLS: abap.). CL_ABAP_CHAR_UTILITIES中有字符常量:如:CR_LF,HORIZONTAL_TAB,NEWLINE等等. ------------------------------------------------- 是否全是数字 if aaa CO '01234567888889 '. --------------------------------------------------- 数学函数 ABAP 代码编辑器中 strlen F1。 ABAP - Keyword documentation ABAP By Theme Built-in Type, Data Objects, and Functions Built-in Functions Mathematical Functions ------------------------------------------------------------ <script type="text/javascript"> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>