ABAP Help Document(17):9.2字符类型数据运算
9.2Character String and Byte String Processing
字符型数据c,n,d,t,string,字节型数据x,xstring。
示例1:CONCATENATE,FIND
"字符型,字节型数据处理 *CONCATENATE {dobj1 dobj2 ...}|{LINES OF itab} * INTO result * [IN {CHARACTER|BYTE} MODE] * [SEPARATED BY sep] * [RESPECTING BLANKS]. *FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern * IN [section_of] dobj *[IN {CHARACTER|BYTE} MODE] *[find_options]. "FIND IN TABLE可以查询内表 "FIND某些情况比CS快; "pattern *{[SUBSTRING] substring} | {REGEX regex} "[section_of] *SECTION [OFFSET off] [LENGTH len] OF "[find_options] *[{RESPECTING|IGNORING} CASE] * [MATCH COUNT mcnt] *{{[MATCH OFFSET moff][MATCH LENGTH mlen]} *|[RESULTS result_tab|result_wa] } *[SUBMATCHES s1 s2 ...] FORM f_character_process_find. DATA:lv_str TYPE string VALUE 'a'. DATA:lt_text TYPE TABLE OF text. DATA:lv_res TYPE string. "链接空白字符 CONCATENATE lv_str lv_str INTO lv_str SEPARATED BY ' '. WRITE:/ lv_str. lv_str = 'a'. CONCATENATE lv_str lv_str INTO lv_str SEPARATED BY space. WRITE:/ lv_str. "RESPECTING BLANKS,itab line链接 APPEND 'hi' TO lt_text. APPEND 'we' TO lt_text. CONCATENATE LINES OF lt_text INTO lv_res RESPECTING BLANKS. WRITE:/ lv_res. "find DATA:lv_count TYPE I. DATA:lv_off TYPE I. DATA:lv_len TYPE I. DATA:lt_result_tab TYPE match_result_tab. DATA:lw_result_wa TYPE match_result. DATA:lv_s1 TYPE string. DATA:lv_s2 TYPE string. DATA:lv_s3 TYPE string. lv_str = 'hey,hi,hello,well,will'. "[SUBSTRING]可选 FIND FIRST OCCURRENCE OF SUBSTRING 'H' "指定匹配section范围 IN SECTION OFFSET 2 LENGTH 5 OF lv_str "字符型模式 IN CHARACTER MODE "忽略大小写 IGNORING CASE "匹配次数,匹配偏移,匹配子串长度 MATCH COUNT lv_count MATCH OFFSET lv_off MATCH LENGTH lv_len. WRITE:/ lv_count,lv_off,lv_len. FIND ALL OCCURRENCES OF 'H' IN SECTION OFFSET 2 LENGTH 8 OF lv_str IGNORING CASE "all occurrence,返回最后一次匹配off MATCH COUNT lv_count MATCH OFFSET lv_off MATCH LENGTH lv_len. WRITE:/ lv_count,lv_off,lv_len. FIND ALL OCCURRENCES OF REGEX '(H.{1,2})(W.{1,3})' IN lv_str IGNORING CASE "匹配结果,line,offset,length,submatches "line在find in table使用,表名行数 "result_wa 保存最后一次匹配结果 RESULTS lt_result_tab "? SUBMATCHES lv_s1 lv_s2. WRITE:/ lv_s1,lv_s2. LOOP AT lt_result_tab INTO lw_result_wa. WRITE:/ lw_result_wa-line,lw_result_wa-offset,lw_result_wa-length. ENDLOOP. ENDFORM.
示例2:REPLACE
*1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern * IN [section_of] dobj WITH new * [IN {CHARACTER|BYTE} MODE] * [replace_options]. "REPLACE IN TABLE 内表数据替换 "[replace_options] *[{RESPECTING|IGNORING} CASE] * [REPLACEMENT COUNT rcnt] * { {[REPLACEMENT OFFSET roff] * [REPLACEMENT LENGTH rlen]} * | [RESULTS result_tab|result_wa] } "result_tab类型REPL_RESULT_TAB and REPL_RESULT *2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new * [IN {CHARACTER|BYTE} MODE]. FORM f_character_process_replace. DATA:lv_result TYPE string VALUE 'welcome,will,hello,hi,hey,weel'. DATA:lv_count TYPE I. DATA:lv_off TYPE I. DATA:lv_len TYPE I. DATA:lt_result_tab TYPE repl_result_tab. DATA:lw_result_wa TYPE repl_result. REPLACE ALL OCCURRENCES OF REGEX '(h.{1})' "被替换字符串 IN lv_result "替换字符串 WITH '**' "替换次数 REPLACEMENT COUNT lv_count "最后一次替换off REPLACEMENT OFFSET lv_off "替换字符串长度 REPLACEMENT LENGTH lv_len. "welcome,will,**llo,**,**y,weel WRITE:/ lv_result,lv_count,lv_off,lv_len. lv_result = 'welcome,will,hello,hi,hey,weel'. REPLACE ALL OCCURRENCES OF REGEX '(h.{1})' IN lv_result WITH '**' REPLACEMENT COUNT lv_count RESULTS lt_result_tab. "welcome,will,**llo,**,**y,weel WRITE:/ lv_result. LOOP AT lt_result_tab INTO lw_result_wa. WRITE:/ lw_result_wa-line,lw_result_wa-offset,lw_result_wa-length. ENDLOOP. "直接replace指定section lv_result = 'welcome,will,hello,hi,hey,weel'. REPLACE SECTION OFFSET 0 LENGTH 8 OF lv_result WITH '***' IN CHARACTER MODE. "***will,hello,hi,hey,weel WRITE:/ lv_result. ENDFORM.
示例3:SHIFT
*SHIFT dobj [ {[places][direction]} | deleting ] * [IN {CHARACTER|BYTE} MODE]. "[places] *{BY num PLACES} | {UP TO substring} "[derection] *[LEFT|RIGHT][CIRCULAR]. "deleting *{ {LEFT DELETING LEADING}| {RIGHT DELETING TRAILING} } mask FORM f_character_process_shift. DATA:lv_result TYPE string VALUE 'XXXX hello you well XXXX'. "左移num places SHIFT lv_result BY 4 PLACES LEFT. WRITE:/ lv_result. "左移,直到指定字符串 SHIFT lv_result UP TO 'you' LEFT. WRITE:/ lv_result. "delte lv_result = 'XXXX hello you well XXXX'. SHIFT lv_result LEFT DELETING LEADING 'X'. WRITE:/ lv_result. SHIFT lv_result RIGHT DELETING TRAILING 'X'. WRITE:/ lv_result. ENDFORM.
示例4:SPLIT
*SPLIT dobj AT sep INTO * { {result1 result2 ...} | {TABLE result_tab} } * [IN {CHARACTER|BYTE} MODE]. FORM f_character_process_split. DATA:lv_source TYPE string VALUE 'welcome;hello;well'. DATA:lv_str1 TYPE string. DATA:lv_str2 TYPE string. DATA:lt_str TYPE TABLE OF string. "拆分到指定字符串 SPLIT lv_source AT ';' INTO lv_str1 lv_str2. "拆分到table SPLIT lv_source AT ';' INTO TABLE lt_str. WRITE:/ lv_str1,lv_str2. LOOP AT lt_str INTO lv_source. WRITE:/ lv_source. ENDLOOP. ENDFORM.
示例5:CONDENSE
*CONDENSE text [NO-GAPS]. FORM f_character_process_condense. DATA:lv_source TYPE string VALUE ' hello welcome hi '. "去掉前后空格 CONDENSE lv_source. WRITE:/ lv_source. lv_source = ' hello welcome hi '. "去掉所有空格 CONDENSE lv_source NO-GAPS. WRITE:/ lv_source. ENDFORM.
示例6:CONVERT TEXT
"CONVERT TEXT text INTO SORTABLE CODE hex. FORM f_character_process_convert. DATA:lv_text1 TYPE C LENGTH 10 VALUE 'E'. DATA:lv_text2 TYPE C LENGTH 10 VALUE 'A'. DATA:lv_hex1 TYPE xstring. DATA:lv_hex2 TYPE xstring. "比较hex CONVERT TEXT lv_text1 INTO SORTABLE CODE lv_hex1. CONVERT TEXT lv_text2 INTO SORTABLE CODE lv_hex2. IF lv_hex1 > lv_hex2. WRITE:/ lv_text1,'大于',lv_text2. ELSE. WRITE:/ lv_text1,'小于',lv_text2. ENDIF. ENDFORM.
示例7:OVERLAY
*OVERLAY text1 WITH text2 [ONLY mask]. "使用text2替换text1中mask指定字符串 FORM f_character_process_overlay. DATA:lv_text1 TYPE string VALUE 'hello wellcome hi'. DATA:lv_text2 TYPE string VALUE 'yeil'. "?没替换 OVERLAY lv_text1 WITH lv_text2. WRITE:/ lv_text1. "将‘hello’替换成‘yeil’ lv_text2 = 'yeil'. OVERLAY lv_text1 WITH lv_text2 ONLY 'hello'. WRITE:/ lv_text1. ENDFORM.
示例8:TRANSLATE
*TRANSLATE text {TO {UPPER|LOWER} CASE} * | {USING mask}. FORM f_character_process_translate. DATA:lv_text TYPE string VALUE 'hello'. "大写 TRANSLATE lv_text TO UPPER CASE. WRITE:/ lv_text. "小写 TRANSLATE lv_text tO LOWER CASE. WRITE:/ lv_text. "mask "H->h,h->H,l->L转换规则,成对 TRANSLATE lv_text USING 'HhhHlL'. WRITE:/ lv_text. ENDFORM.
示例9:WRITE
*WRITE {source|(source_name)} TO destination * [format_options]. *[LEFT-JUSTIFIED|CENTERED|RIGHT-JUSTIFIED] *{ { [EXPONENT exp] * [NO-GROUPING] * [NO-SIGN] * [NO-ZERO] * [CURRENCY cur] * { { [DECIMALS dec] * [ROUND scale] } | [UNIT unit] * } } * | { [ENVIRONMENT TIME FORMAT] * [TIME ZONE tz] } * [STYLE stl] } * [USING { {NO EDIT MASK}|{EDIT MASK mask} }] * [ DD/MM/YY | MM/DD/YY * | DD/MM/YYYY | MM/DD/YYYY * | DDMMYY | MMDDYY * | YYMMDD ] "[EXPONENT exp]:ENVIRONMENT TIME FORMAT,TIME ZONE不能同时使用 "[NO-GROUPING],[NO-SIGN],[NO-ZERO]:ENVIRONMENT TIME FORMAT,TIME ZONE不能同时使用 "[CURRENCY cur]:ENVIRONMENT TIME FORMAT,TIME ZONE,STYLE不能同时使用 " 数据类型不能为decfloat16,decfloat34; " 默认两位小数,TCURC币别表,TCURX表,指定小数位数; "[DECIMALS dec]:ENVIRONMENT TIME FORMAT,TIME ZONE,UNIT不能同时使用 "[ROUND scale]:ENVIRONMENT TIME FORMAT, TIME ZONE,UNIT,STYLE不能同时使用 "[UNIT unit]:ENVIRONMENT TIME FORMAT,TIME ZONE,DECIMALS, ROUND, STYLE不能同时使用 "[STYLE stl]:CURRENCY, DD/MM/YY, YYMMDD, ROUND, " ENVIRONMENT TIME FORMAT, TIME ZONE,UNIT不能同时使用 " CL_ABAP_FORMAT类style FORM f_character_process_write. "设定位置 DATA:lv_source TYPE C LENGTH 20. lv_source = 'Position'. WRITE lv_source TO lv_source LEFT-JUSTIFIED. WRITE:/ lv_source. lv_source = 'Position'. WRITE lv_source TO lv_source RIGHT-JUSTIFIED. WRITE:/ lv_source. lv_source = 'Position'. WRITE lv_source TO lv_source CENTERED. WRITE:/ lv_source. "EXPONENT,设定指数 DATA:lv_f TYPE f. DATA:lv_f_text TYPE C LENGTH 6. lv_f = '123.456'. WRITE lv_f TO lv_f_text EXPONENT 2. WRITE:/ lv_f_text. "[NO-GROUPING], "数据类型:(s), i, p, decfloat16, or decfloat34 "千分位符号 DATA:lv_decf16 TYPE decfloat16. DATA:lv_decf_text TYPE C LENGTH 20. lv_decf16 = '123456789.123'. WRITE lv_decf16 TO lv_decf_text NO-GROUPING. WRITE:/ lv_decf16,lv_decf_text. "[NO-SIGN] "数据类型:(s), i, p, decfloat16, decfloat34, or f "正负号 DATA:lv_i TYPE I. DATA:lv_i_text TYPE C LENGTH 10. lv_i = '-123'. WRITE lv_i TO lv_i_text NO-SIGN. WRITE:/ lv_i,lv_i_text. "[NO-ZERO] "有效数据类型:c, n, or string DATA:lv_n TYPE N LENGTH 10. DATA:lv_n_text TYPE C LENGTH 10. lv_n = '123'. WRITE lv_n TO lv_n_text NO-ZERO. WRITE:/ lv_n_text. WRITE lv_n TO lv_n_text. WRITE:/ lv_n_text. "[DECIMALS dec] "有效数据类型:(b, s), i, p, decfloat16, decfloat34, or f. DATA:lv_dec TYPE f. DATA:lv_dec_text TYPE C LENGTH 20. lv_dec = '123.456'. WRITE lv_dec TO lv_dec_text DECIMALS 2. WRITE:/ lv_dec,lv_dec_text. "[UNIT unit] "T006表pcs,decan字段,三位小数 DATA:lv_unit TYPE P LENGTH 10 DECIMALS 4. DATA:lv_unit_text TYPE C LENGTH 10. lv_unit = '1234.5678'. WRITE lv_unit TO lv_unit_text UNIT 'PCS'. WRITE:/ lv_unit,lv_unit_text. "[USING { {NO EDIT MASK}|{EDIT MASK mask} }] DATA:lv_time TYPE sy-uzeit. DATA:lv_time_text TYPE C LENGTH 30. lv_time = sy-uzeit. WRITE lv_time TO lv_time_text USING NO EDIT MASK. WRITE:/ lv_time_text. WRITE lv_time TO lv_time_text USING EDIT MASK 'RRthe mask__:__:__'. WRITE:/ lv_time_text. WRITE lv_time TO lv_time_text USING EDIT MASK 'LLthe mask__:__:__'. WRITE:/ lv_time_text. DATA:lv_date TYPE sy-datum. DATA:lv_date_text TYPE C LENGTH 30. lv_date = sy-datum. WRITE lv_date TO lv_date_text YYMMDD. WRITE:/ lv_date,lv_date_text. ENDFORM.
示例10:SET BIT,GET BIT
"SET BIT bitpos OF byte_string [TO val]. "GET BIT bitpos OF byte_string INTO val. FORM f_character_process_bit. DATA:lv_bit TYPE xstring. DATA:lv_val TYPE I. lv_bit = '1B'. SET BIT 1 OF lv_bit TO lv_val. WRITE:/ lv_bit. GET BIT 1 OF lv_bit INTO lv_val. WRITE:/ lv_val. ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/14691221.html