REPORT z_overlay_001 NO STANDARD PAGE HEADING MESSAGE-ID mg.
WRITE : /,'********************拆分字符串split***************'.
*语法:
* SPLIT <c> AT <del> INTO <c1> ... <cn>.
*用法:
* 将字符串<c>根据字符串<del>拆分成字符串<c1>...<cn>.
PERFORM frm_split.
WRITE : /,'****************根据长度截取字符***************'.
*函数名称:CONVERT_STRING_TO_TABLE
*输入参数:字符串<str> 、截取长度<len>、接收表<itb>
PERFORM frm_convert_string_to_table.
WRITE : /,'******************连接字符串***************'.
*语法:
* CONCATENATE <c1> ... <cn> INTO <c> [SEPARATED BY <s>].
* 1.如果<s>未写,直接将<c1>....<cn>拼接成<c>
* 2.如果<s>有写,将<c1>....<cn>拼接成<c> 其中插入<s>
PERFORM frm_concatenate.
WRITE : /,'******************获得字符串长度***************'.
*[COMPUTE] <n> = STRLEN( <c> ).
PERFORM frm_strlen.
WRITE : /,'******************压缩字段内容***************'.
*语法:
* CONDENSE <c> [NO-GAPS].
*该语句去除字段 <c> 中的前导空格并用一个空格替换其它空格序列 。
*结果是左对齐单词, 每个单词用空格隔开。
*如果指定附 加的 NO-GAPS, 则去除所有空格。
PERFORM frm_condense.
WRITE : /,'******************搜索字符串***************'.
*语法:
* SEARCH <c> FOR <str> <options>.
*SEARCH 1
* 该语句在字 段 <c> 中搜索<str> 中的字符串 。
* 如果成功 ,则将 SY-SUBRC 的返回代码 值设置为0,并将 SY-FDPOS 设置为字段 <c> 中该字符串的偏移量。
* 否则将 SY-SUBRC 设置为4。
* 搜索串 <str> 可为下列格 式之一:
* <str> 目 的
* ---------------------------------
* 1. <pattern> 搜 索 <pattern>(任何字符顺序)。忽略尾部空格。
* 2. .<pattern>. 搜 索 <pattern> ,但是不忽略尾部空格 。
* 3. *<pattern> 搜 索以 <pattern> 结尾的词。
* 4. <pattern>* 搜 索以 <pattern> 开始的词。
* 单词之间用 空格、逗号 、句号、分 号、冒号、 问号、叹号 、括号、斜 杠、加号和等号等分隔 。
PERFORM frm_search_01.
*SEARCH 2
*ABBREVIATED :
* 在字段 <c> 中搜索包含 <str> 中的单词,
* 其中字符可以被其它字符隔开,
* 但是单词和字符串的第一个字母必须相同 。
*STARTING AT <n1>:
* 在字段 <c> 中搜索从 <n1> 开始的 <str> 。
* 结果 SY-FDPOS 参照相对于 <n1> 的偏移量而不是字段的开始。
*ENDING AT <n2>:
* 在字段 <c> 搜索 <str> 直到位置 <n2>。
*AND MARK :
* 如果找到搜索串,则将搜索串中的所有字符
* (使用 ABBREVIATED 时的所有字 符)转换为大写形式。
PERFORM frm_search_02.
WRITE : /,'********************覆盖字符字段***************'.
*语法:
* OVERLAY <c1> WITH <c2> [ONLY <str>].
*用法:
*1. <c1>、<c2>、<str> 可为变量
*2.如果<str>未指定,用<c2>对应的位置替换<c1>的空格,<c2>没有任何变化,如果没有空格,不替换
*3.如果<str>指定,用<c2>对应的位置替换<c1>的<str>单个字符,<c2>没有任何变化
*4.如果 <c1> 比 <c2> 长,则只覆 盖 <c2> 中的长度。
PERFORM frm_overlay.
WRITE : /,'********************替换字段内容***************'.
*语法:
* REPLACE <str1> WITH <str2> INTO <str> [LENGTH <l>].
*用法:
* 1.<str1>、<str2>、<l>可以为变量
* 2.如果<1>指定,将<str>中的<str1>的前<1>个字段替换为<str2>
* 3.如果<1>未指定,则将<str>中的<str1>替换未<str2>
PERFORM frm_replace.
*&---------------------------------------------------------------------*
*& Form FRM_SPLIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_split .
DATA: string(60),
p1(20) VALUE '',
p2(20) VALUE '',
p3(20) VALUE '',
p4(20) VALUE '',
del(3) VALUE '***'.
string = ' Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5'.
WRITE string.
SPLIT string AT del INTO p1 p2 p3 p4.
WRITE: /'P1:' , p1.
WRITE: /'P2:' , p2.
WRITE: /'P3:' , p3.
WRITE: /'P4:' , p4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CONVERT_STRING_TO_TABLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_convert_string_to_table .
DATA : str TYPE string VALUE '1234567890abcdefghigklmn',
len TYPE i VALUE 4,
itb TYPE TABLE OF string WITH HEADER LINE.
WRITE :/,'str:',str,'len:',len.
CALL FUNCTION 'CONVERT_STRING_TO_TABLE'
EXPORTING
i_string = str
i_tabline_length = len
TABLES
et_table = itb.
LOOP AT itb.
WRITE :/,'序号',sy-tabix,'itb:',itb.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CONCATENATE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_concatenate .
DATA: c1(10) VALUE 'Sum',
c2(3) VALUE 'mer',
c3(5) VALUE 'holi ',
c4(10) VALUE 'day',
c5(30),
sep(3) VALUE ' - '.
CONCATENATE c1 c2 c3 c4 INTO c5.
WRITE / c5.
CONCATENATE c1 c2 c3 c4 INTO c5 SEPARATED BY sep.
WRITE / c5.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_STRLEN
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_strlen .
DATA: int TYPE i,
word1(20) VALUE '12345',
word2(20),
word3(20) VALUE ' 4 '.
int = strlen( word1 ).
WRITE / int.
int = strlen( word2 ).
WRITE / int.
int = strlen( word3 ).
WRITE / int.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CONDENSE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_condense .
DATA: string(25) VALUE ' one two three four',
len TYPE i.
len = strlen( string ).
WRITE: / string, '!'.
WRITE: / 'Length: ', len.
CONDENSE string.
len = strlen( string ).
WRITE: string, '!'.
WRITE: / 'Length: ', len.
CONDENSE string NO-GAPS.
len = strlen( string ).
WRITE: string, '!'.
WRITE: / 'Length: ', len.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SEARCH_01
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_search_01 .
DATA string(30) VALUE 'This is a little sentence.'.
WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.
ULINE /1(26).
SEARCH string FOR 'X'.
WRITE: / 'X', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string FOR 'itt '.
WRITE: / 'itt ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string FOR '.e .'.
WRITE: / '.e .', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string FOR '*e'.
WRITE: / '*e ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string FOR 's*'.
WRITE: / 's* ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SEARCH_02
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_search_02 .
DATA: string(30) VALUE 'This is a fast first example.',
pos TYPE i,
off TYPE i.
WRITE / string.
SEARCH string FOR 'ft' ABBREVIATED.
WRITE: / 'SY-FDPOS:', sy-fdpos.
pos = sy-fdpos + 2.
SEARCH string FOR 'ft' ABBREVIATED STARTING AT pos AND MARK.
WRITE / string.
WRITE: / 'SY-FDPOS:', sy-fdpos.
off = pos + sy-fdpos - 1.
WRITE: / 'Off:', off.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_OVERLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_overlay .
DATA : str1 TYPE string,
str2 TYPE string.
str1 = '1 2 3'.
str2 = '456 '.
WRITE : /,'VALUE :' ,'STR1:' ,str1,'STR2:' ,str2.
OVERLAY str1 WITH str2.
WRITE : /, 'OVERLAY :','STR1:' ,str1,'STR2:' ,str2.
str1 = '1 2 3'.
str2 = '456'.
WRITE : /,'VALUE :' ,'STR1:' ,str1,'STR2:' , str2.
OVERLAY str1 WITH str2 ONLY '12'.
WRITE : /, 'OVERLAY-only 12 :','STR1:' ,str1,'STR2:' ,str2.
str1 = '123'.
str2 = '456'.
WRITE : /,'VALUE :', 'STR1:' ,str1,'STR2:' ,str2.
OVERLAY str1 WITH str2.
WRITE : /, 'OVERLAY :','STR1:' ,str1,'STR2:' ,str2.
str1 = '1 2 3'.
str2 = '45678'.
WRITE : /,'VALUE :' ,'STR1:' ,str1,'STR2:' ,str2.
OVERLAY str1 WITH str2 ONLY '13'.
WRITE : /, 'OVERLAY-only 13 :','STR1:' ,str1,'STR2:' ,str2.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_REPLACE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_replace .
DATA: t(10) VALUE 'abcdefghij',
string LIKE t,
str1(4) VALUE 'cdef',
str2(4) VALUE 'klmn',
str3(2) VALUE 'kl',
str4(6) VALUE 'klmnop',
len TYPE i VALUE 2.
string = t.
WRITE / string.
REPLACE str1 WITH str2 INTO string.
WRITE: / ,'str1:' , str1 ,'str2:' ,str2, 'string:' ,string.
string = t.
REPLACE str1 WITH str2 INTO string LENGTH len.
WRITE: / ,'str1:' , str1 ,'str2:' ,str2, 'string:' ,string ,'len:' ,len.
string = t.
REPLACE str1 WITH str3 INTO string.
WRITE: / ,'str1:' , str1 ,'str3:' ,str3, 'string:' ,string .
string = t.
REPLACE str1 WITH str4 INTO string.
WRITE: / ,'str1:' , str1 ,'str4:' ,str4, 'string:' ,string .
ENDFORM.