sap abap 字符串处理

ECC5 help:Byte und ZeichenkettenveRARBEITUNG/PROCESSING character strings
例子参考ABAPdocu
函数查询:
(帮助)
ABAP - The SAP Programming Language/
ABAP - By Theme/
Process Internal Data/Byte String and Character String Processing
CONCATENATE 连接字符串 [SEPARATED BY 分割符] 
find 
SPLIT 拆分子串
------------------------------------------------------
取前八位
data a type c(30).
a = '88888888abcdefg'.
a = a(6). "取前6位
a = a+6(1). "取第7位
a = a+6 . "取第六位后的所有字符
-------------------------------------------------------
拆分
split XXX at into table XXX
SPLIT dobj AT sep INTO 
      { {result1 result2 ...} | {TABLE result_tab} } 
      [IN {BYTE|CHARACTER} MODE]. 
--------------------------------------------------------
拼接
DATA NAME (30). 
        NAME(10)    = '    Dr.', 
        NAME+10(10) = 'Michael', 
        NAME+20(10) = 'Hofmann'. 
CONDENSE NAME. 
WRITE NAME. 
-------------------------------------------------------
去空格
CONDENSE
-------------------------------------------------------
查找
Search for all occurrences of the string "now" in a string literal using a WHILE loop. After every successful search, the search range is redefined to start after the found location. This enables you to find all occurrences of the search string even in releases before 7.0. 
DATA: patt TYPE string VALUE `now`, 
      text TYPE string, 
      off  TYPE i, 
      moff TYPE i, 
      mlen TYPE i. 
off = 0. 
WHILE sy-subrc = 0. 
  FIND patt IN SECTION OFFSET off OF 
       `Everybody knows this is nowhere` 
       MATCH OFFSET moff 
       MATCH LENGTH mlen. 
  IF sy-subrc = 0. 
    WRITE / moff. 
    off = moff + mlen. 
  ENDIF. 
ENDWHILE. 
------------------------------------------------------------
补零
数字前补零
用途:
在查语句中,'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等等.
CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE-----(utf-16le')的文件头
CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_UTF8-------(utf-8)的文件头
例如:转unicode的时候:
*  W_TYPE = 'APPLICATION/MSEXCEL;charset=utf-16le'.
  W_TYPE = 'APPLICATION/MSEXCEL;charset=utf-8'.
*  W_TYPE = '  =utf8'.
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT     = L_STRING
      MIMETYPE = W_TYPE
    IMPORTING
      BUFFER   = L_XSTRING
    EXCEPTIONS
      FAILED   = 1
      OTHERS   = 2.
  IF SY-SUBRC EQ 0.
*    CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE(utf-16le')的文件头
    CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_UTF8(utf-8)的文件头....
  endif.
-------------------------------------------------
是否全是数字
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
------------------------------------------------------------
(站长注:本文内容不完整 未分类 欢迎提供建议 争取达到方便快速查阅的效果,免得写代码的时候被字符操作这样的小问题耽误时间)

1.字符串移位
语法:
        SHIFT [BY PLACES] []
         : (1).空白, 字符串往左移一位
                 (2).LEFT, 字符串往左移 n 位
                 (3).RIGHT, 字符串往右移 n 位
                 (4).CIRCULAR: 字符串以环状方式移位
        Example:
                 DATA STRING(10) VALUE ‘ABCDEFGHIJ’.
                 SHIFT STRING. “得到 BCDEFGHI’
                 SHIFT STRING BY 2 PLACES RIGHT. “得到 ABCDEFGH
2.取代字符串内容
语法:
        REPLACE WITH INTO
        将字符串 中的 来取代
        Example:
                 DATA: STRING(10) VALUE ‘ABCDEFGHI’,
                         STR1(3) VALUE ‘DEF’,
                         STR2(3) VALUE ‘123’.
                 REPLACE STR1 WITH STR2 INTO STRING.
                 WRITE / STRING.   “得到 ABC123GHI
3.大小写的转换
语法:
        TRANSLATE TO UPPER CASE. “转成大写
        TRANSLATE TO LOWER CASE. “转成小写
4.在字符串中寻找部分字符串
语法:
       SEARCH FOR
       Example:
                DATA STRING(10) VALUE ‘ABCDEFGHIJ’.
                会回存至两个变量, SY-SUBRC 和 SY-FDPOS, 若找到则 SY-SUBRC 为 0
                SY-FDPOS 存开始位叠, 若找不到则 SY-SUBRC为 4, SY-FDPOS为 0
5.字符串长度
STRLEN()
Example:
            INT = STRLEN(‘XYZABC’). “得到 6
            INT = STRLEN(‘ABC    ’ ). “得到 3
6.取部分字符串
[+][]
Example:
           DATA T(10) VALUE ‘ABCDEFGHIJ’.
           WRITE / T+2(4). “得到 CDEF

OVERLAY w_num WITH '000000000000'.
SHIFT c LEFT DELETING LEADING c1. 
Variant 5 
SHIFT c RIGHT DELETING TRAILING c1.

the options tab under LocalData. 
If it didn't work, find the .mdb file like SAP_local*.mdb, the path of which should be available in the options tab under LocalData (Directory of local data). 
It is always possible to delete the file, so that when the SAP frontend is restarted, it creates the file again and history is back again. 
Please make sure to uncheck the enable low speed connection as the history won't work if this was 
C:/Documents and Settings/Administrator/SapWorkDir
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'.

你可能感兴趣的:(SAP,ABAP技术)