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'
1.字符转ASCII
URL_ASCII_CODE_GET
特点:根据这个算法可以中文转ASCII
DATA: char_cd(2) TYPE c.
CALL FUNCTION 'URL_ASCII_CODE_GET'
EXPORTING
trans_char = 'A'
IMPORTING
char_code = char_cd.
WRITE /: char_cd.
2. 利用cl_rpe_convert字符与ASCII互转
DATA: str TYPE string ,
xstr TYPE xstring,
l_con TYPE REF TO cl_rpe_convert .
START-OF-SELECTION.
CREATE OBJECT l_con.
CALL METHOD l_con->string_to_xstring
EXPORTING
input = 'ABC'
IMPORTING
output = xstr.
CALL METHOD l_con->xstring_to_string
EXPORTING
input = '426172727942'
IMPORTING
output = str.
WRITE / : xstr ,str.
3. 特殊字符处理 利用类CL_ABAP_CHAR_UTILITIES在字符串中加入tab字符,回车换行符,换页符
DATA: str TYPE string ,
xstr TYPE xstring,
l_con TYPE REF TO cl_rpe_convert .
START-OF-SELECTION.
CREATE OBJECT l_con.
str = cl_abap_char_utilities=>cr_lf.
CALL METHOD l_con->string_to_xstring
EXPORTING
input = str
IMPORTING
output = xstr.
CALL METHOD l_con->xstring_to_string
EXPORTING
input = '4213'
IMPORTING
output = str.
WRITE / : xstr ,str.
字符串加回车换行TAB符号
字符串加回车换行TAB符号
所有字符在下面的类中:
cl_abap_char_utilities=>horizontal_tab — 09
cl_abap_char_utilities=>CR_LF ———- 0D0A
cl_abap_char_utilities=>VERTICAL_TAB — 0B
cl_abap_char_utilities=>NEWLINE —- 0A
cl_abap_char_utilities=>FORM_FEED —- 0C
cl_abap_char_utilities=>BACKSPACE — 08
如果是要单独取得回车或者换行(不是回车加换行),可以采用:
cl_abap_char_utilities=>CR_LF(1)
cl_abap_char_utilities=>CR_LF 1(1)
从EXCEl文件中传上的文件的回车符就是cl_abap_char_utilities=>CR_LF 1(1)
DATA: crlf(2) TYPE c.
crlf = cl_ABAP_char_utilities=>cr_lf. "换行符
在字符串中加入回车换行或TAB字符,在其他语言可以使用$13$10的ASCII码进行插入.而在abap里却出错,为了解决此问题,请使用sap的类CL_ABAP_CHAR_UTILITIES.
CL_ABAP_CHAR_UTILITIES中有字符常量:如:CR_LF,HORIZONTAL_TAB,NEWLINE等等.
例:
DATA: crlf(2) TYPE c.
DATA: l_html(4096) TYPE C.
crlf = cl_ABAP_char_utilities=>cr_lf.
CONCATENATE l_html crlf '_FUNCTION'
'30'
'WWW_GET_REPORT' '>' INTO l_html.
----
调用方法产生文件路径对话框
DATA: p_file LIKE rlgrap-filename.
FIELD-SYMBOLS <fs> TYPE file_table.
FIELD-SYMBOLS <f>.
DATA: f_table TYPE filetable.
DATA: rc TYPE i.
DATA: user_action TYPE i.
DATA: l_index TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = '请选择要汇入的文件'
file_filter = 'Excel files (*.XLS)|*.XLS'
initial_directory = 'D:\'
CHANGING
file_table = f_table
rc = rc
user_action = user_action
EXCEPTIONS
cntl_error = 1
file_open_dialog_failed = 2
error_no_gui = 3.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS cntl_system_error = 1
cntl_error = 2.
CLEAR p_file.
READ TABLE f_table ASSIGNING <fs> INDEX 1.
IF sy-subrc = 0.
p_file = <fs>-filename.
ENDIF.
在编写SAP程序时,程序员经常需要将常字符串进行分割,在字符串中加入回车换行或TAB字符,在其他语言可以使用$13$10的ASCII码进行插入.而在abap里却出错,为了解决此问题,请使用sap的类CL_ABAP_CHAR_UTILITIES.
CL_ABAP_CHAR_UTILITIES中有字符常量:如:CR_LF,HORIZONTAL_TAB,NEWLINE等等.
下面给出字符串中加入回车换行的示例:
DATA: crlf(2) TYPE c.
DATA: l_html(4096) TYPE C.
crlf = cl_ABAP_char_utilities=>cr_lf.
CONCATENATE l_html crlf '_FUNCTION'
'30'
'WWW_GET_REPORT' '>' INTO l_html.
string的标准函数
查找: FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern IN dobj
替换: REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern IN dobj WITH new
REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
移动:SHIFT c [BY n PLACES] [mode]
拼接:CONCATENATE c1 ... cn INTO c [SEPARATED BY s]
分割:SPLIT c AT del INTO c1 ... cn.
大小写转换:TRANSLATE text TO UPPER CASE.
TRANSLATE text TO LOWER CASE.
字符串覆盖:OVERLAY c1 WITH c2 [ONLY str].
压缩字符串:CONDENSE c [NO-GAPS].
系统扩张的string函数(函数组CSTR)
STRING_LENGTH
STRING_CONCATENATE
STRING_MOVE_RIGHT
STRING_REVERSE
STRING_SPLIT
STRING_CENTER
TEXT_SPLIT
STRING_SPLIT_AT_POSITION
OVERLAY_BYTE
SJIS_SBC_TO_DBC
SJIS_DBC_TO_SBC