DATA: LV_LENGTH TYPE I, LV_STRING1 TYPE STRING VALUE 'Learn from yesterday, live for today, hope for tomorrow.', LV_STRING2 TYPE STRING VALUE `Don't look back, just haven't found leave your reasons.`.
"Learn from yesterday, live for today, hope for tomorrow. "上述短文的长度为: 54 "Don't look back, just haven't found leave your reasons. "上述短文的长度为: 54
(2)DESCRIBE
通过这种方式只能获取固定长度变量的定义的长度,无法获取到变量中存在的值的长度。拥有两种获取长度的方式。一种是获取位的长度(IN BYTE MODE);一种是获取变量定义的字符长度(IN CHARACTER MODE)。但是获取字符长度的定一个是无法应用于String类型的变量。因为其是不定长的,但是获取位长度的方式可以获取到String类型变量的系统默认位长度为8.
DATA: LV_LENGTH TYPE I, LV_STRING1 TYPE STRING VALUE 'Learn from yesterday, live for today, hope for tomorrow.', LV_STRING2 TYPE STRING VALUE `Don't look back, just haven't found leave your reasons.`. WRITE:/ 'DESCRIBE FIELD 语法测试,第一个语句为String类型,第二个为Char80'. WRITE:/.
"这种方式获取的是定义的变量长度,若值的长度小于定义长度,系统会自动填充到指定长度 DESCRIBE FIELD LV_STRING1 LENGTH LV_LENGTH IN BYTE MODE. WRITE:/ LV_STRING1. WRITE:/ '位长度,上述短文的长度为: ',LV_LENGTH.
DESCRIBE FIELD LV_STRING2 LENGTH LV_LENGTH IN BYTE MODE. WRITE:/ LV_STRING2. WRITE:/ '位长度,上述短文的长度为: ',LV_LENGTH.
"这种形式的获取长度方式无法获取String类型变量的长度,所以只能获取固定长度类型变量的定义时的长度 DESCRIBE FIELD LV_STRING2 LENGTH LV_LENGTH IN CHARACTER MODE. WRITE:/ LV_STRING2. WRITE:/ '字符长度,上述短文的长度为: ',LV_LENGTH.
"运行结果: "DESCRIBE FIELD 语法测试,第一个语句为String类型,第二个为Char80
"Learn from yesterday, live for today, hope for tomorrow. "位长度,上述短文的长度为: 8 "Learn from yesterday, live for today, hope for tomorrow. "位长度,上述短文的长度为: 160 "Don't look back, just haven't found leave your reasons. "字符长度,上述短文的长度为: 80
DATA: LV_FIND TYPE STRING VALUE 'AA', LV_STRING0 TYPE STRING VALUE 'DHFIOASDHFJKAASHDFIUASHDFPAASDFJHASIFHDFJKLGSDJIKFGN', LV_STRING4 TYPE STRING VALUE 'AHDFGIUFHASIOUDFHASDHFFHFHAIOSDFHFHAKLSJDFHKJLASHDFD'.
FIND LV_FIND IN LV_STRING0. IF SY-SUBRC EQ 0. WRITE:/ '查找到了'. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 查找到了
FIND LV_FIND IN LV_STRING4. IF SY-SUBRC EQ 0. WRITE:/ '查找到了'. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 未查找到
(2)查找位置与长度信息 MATCH OFFSET / LENGTH
可以使用MATCH OFFSET 和 MATCH LENGTH 关键词获取查找内容的位置与长度。若未查找数据,则不会更新MATCH OFFSET 和 MATCH LENGTH 关键词后接受位置信息与长度信息的变量,所以在使用前请清空用于存放位置信息与长度信息的变量。
"沿用上面的变量 DATA: LV_POSTION TYPE I, LV_LENGTH TYPE I. FIND LV_FIND IN LV_STRING0 MATCH OFFSET LV_POSTION MATCH LENGTH LV_LENGTH. IF SY-SUBRC EQ 0. WRITE:/ '位置为: ', LV_POSTION, '长度为: ', LV_LENGTH. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 位置为: 12 , 长度为: 2
CLEAR: LV_POSTION,LV_LENGTH. "使用之前需要清空位置与长度变量,因为查找失败之后并不会修改LV_POSTION,LV_LENGTH的变量值,这两个变量还是原先的值 FIND LV_FIND IN LV_STRING0 MATCH OFFSET LV_POSTION MATCH LENGTH LV_LENGTH. IF SY-SUBRC EQ 0. WRITE:/ '位置为: ', LV_POSTION, '长度为: ', LV_LENGTH. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 未查找到
"沿用上面的变量 DATA: LV_COUNT TYPE I. FIND ALL OCCURRENCES OF LV_FIND IN LV_SRING0 MATCH COUNT LV_COUNT. IF SY-SUBRC EQ 0. WRITE:/ '出现次数为: ', LV_COUNT. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 出现次数为: 2
CLEAR: LV_COUNT. FIND ALL OCCURRENCES OF LV_FIND IN LV_STRING4 MATCH COUNT LV_COUNT. IF SY-SUBRC EQ 0. WRITE:/ '出现次数为: ', LV_COUNT. ELSE. WRITE:/ '未查找到'. ENDIF. "运行结果: 未查找到
REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern IN [section_of] dobj WITH new [IN {CHARACTER|BYTE} MODE] [replace_options].
REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new [IN {CHARACTER|BYTE} MODE].
3.语法解释与应用
(1)替换首次出现的内容 FIRST OCCURRENCE(默认)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM', LV_REPLACE TYPE STRING VALUE 'QWE'. *----------------------------Logic-------------------------------------* "替换首次出现的内容 "REPLACE LV_REPLACE IN LV_STRING WITH ''. 上下两句效果相同 REPLACE FIRST OCCURRENCE OF LV_REPLACE IN LV_STRING WITH ''. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. "运行结果: "RTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM
(2)替换所有出现的内容 ALL OCCURRENCES
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM', LV_REPLACE TYPE STRING VALUE 'QWE'. *----------------------------Logic-------------------------------------* "替换首次出现的内容 REPLACE ALL OCCURRENCES OF LV_REPLACE IN LV_STRING WITH ''. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. "运行结果: "RTYUIOPASDFGHJKLZXCVBNM|RTYUIOPASDFGHJKLZXCVBNM
(3)以字符模式 IN CHARACTER MODE (默认)
按照字符的形式,逐个替换对应的内容。效果其实和上面的一致。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM', LV_REPLACE TYPE STRING VALUE 'QWE'. *----------------------------Logic-------------------------------------* "以字符模式 REPLACE LV_REPLACE IN LV_STRING WITH '' IN CHARACTER MODE. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. "运行结果: "RTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM', LV_REPLACE TYPE STRING VALUE 'QWE'. *----------------------------Logic-------------------------------------* "根据字符位置与长度替换 "从5号字符开始,将之后的8个字符替换为空 REPLACE SECTION OFFSET 5 LENGTH 8 OF LV_STRING WITH ''. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. "运行结果: "QWERTFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM "从0号字符开始,将之后的3个字符替换为123 REPLACE SECTION OFFSET 0 LENGTH 3 OF LV_STRING WITH '123'. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. "运行结果: "123RTFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM|QWERTYUIOPASDFGHJKLZXCVBNM', LV_REPLACE TYPE STRING VALUE 'QWE', LV_COUNT TYPE I, LV_OFFSET TYPE I, LV_LENGTH TYPE I. *----------------------------Logic-------------------------------------* REPLACE ALL OCCURRENCES OF LV_REPLACE IN LV_STRING WITH '' "替换次数 REPLACEMENT COUNT LV_COUNT "最后一次替换位置 REPLACEMENT OFFSET LV_OFFSET "替换字符串的长度 REPLACEMENT LENGTH LV_LENGTH. IF SY-SUBRC NE 0. "替换失败 MESSAGE S000 WITH '替换失败' DISPLAY LIKE GC_E. ENDIF. WRITE: LV_STRING. WRITE: / '替换次数:', LV_COUNT, / '最后一次替换位置:',LV_OFFSET, / '替换字符串的长度:',LV_LENGTH. "运行结果: "RTYUIOPASDFGHJKLZXCVBNM|RTYUIOPASDFGHJKLZXCVBNM "替换次数:2 "最后一次替换位置:24 "替换字符串的长度:0
*--------------------------Variables-----------------------------------* DATA: LV_CHAR TYPE CHAR10 VALUE '113'. *----------------------------Logic-------------------------------------* UNPACK LV_CHAR TO LV_CHAR. WRITE: LV_CHAR.
TRANSLATE text {TO {UPPER|LOWER} CASE} | {USING mask}.
3.语法解释与应用
(1)切换大写 UPPER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWErty', LV_STRUPPER TYPE STRING VALUE 'QWERTY', LV_STRLOWER TYPE STRING VALUE 'qwerty'. *----------------------------Logic-------------------------------------* "大写切换 TRANSLATE LV_STRING TO UPPER CASE. TRANSLATE LV_STRUPPER TO UPPER CASE. TRANSLATE LV_STRLOWER TO UPPER CASE.
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWErty', LV_STRUPPER TYPE STRING VALUE 'QWERTY', LV_STRLOWER TYPE STRING VALUE 'qwerty'. *----------------------------Logic-------------------------------------* "小写切换 TRANSLATE LV_STRING TO LOWER CASE. TRANSLATE LV_STRUPPER TO LOWER CASE. TRANSLATE LV_STRLOWER TO LOWER CASE.
*--------------------------Variables-----------------------------------* DATA: LV_STRING TYPE STRING VALUE 'QWErty', LV_STRUPPER TYPE STRING VALUE 'QWERTY', LV_STRLOWER TYPE STRING VALUE 'qwerty'. *----------------------------Logic-------------------------------------* "字符切换 "将大写的Q转换为A,将小写的r转换为b "将小写的q转换为a,将大写的R转换为B TRANSLATE LV_STRING USING 'QAqaRBrb'. TRANSLATE LV_STRUPPER USING 'QAqaRBrb'. TRANSLATE LV_STRLOWER USING 'QAqaRBrb'.
*--------------------------Variables-----------------------------------* DATA: BEGIN OF LS_STRUCTURE, TEXT1 TYPE C LENGTH 20 VALUE 'Hello', TEXT2 TYPE C LENGTH 20 VALUE 'World.', TEXT3 TYPE C LENGTH 20 VALUE 'l am an', TEXT4 TYPE C LENGTH 20 VALUE 'ABAP', TEXT5 TYPE C LENGTH 20 VALUE 'programmer.', END OF LS_STRUCTURE. DATA: LV_TEXT TYPE STRING, LV_STRING TYPE STRING VALUE ` QWE `, LV_STRNUL TYPE STRING VALUE ` Q W E `. *----------------------------Logic-------------------------------------* LV_TEXT = LS_STRUCTURE.
WRITE: / '转换后:', / LV_TEXT, / LV_STRING, / LV_STRNUL. "下图中的中括号中间的值为实际值,前后两端都是带有空格的 "转换前: "Hello World. l am an ABAP programmer. "[ QWE ] "[ Q W E ] "转换后: "Hello World. l am an ABAP programmer. "QWE "Q W E
*--------------------------Variables-----------------------------------* DATA: BEGIN OF LS_STRUCTURE, TEXT1 TYPE C LENGTH 20 VALUE 'Hello', TEXT2 TYPE C LENGTH 20 VALUE 'World.', TEXT3 TYPE C LENGTH 20 VALUE 'l am an', TEXT4 TYPE C LENGTH 20 VALUE 'ABAP', TEXT5 TYPE C LENGTH 20 VALUE 'programmer.', END OF LS_STRUCTURE. DATA: LV_TEXT TYPE STRING, LV_STRING TYPE STRING VALUE ` QWE `, LV_STRNUL TYPE STRING VALUE ` Q W E `. *----------------------------Logic-------------------------------------* LV_TEXT = LS_STRUCTURE.
CONCATENATE {dobj1 dobj2 ...}|{LINES OF itab} INTO result [IN {CHARACTER|BYTE} MODE] [SEPARATED BY sep] [RESPECTING BLANKS].
3.语法解释与应用
(1)拼接语句 CONCATENATE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
*--------------------------Variables-----------------------------------* DATA: LV_STR1 TYPE STRING VALUE 'Hello', LV_STR2 TYPE STRING VALUE 'World.', LV_STRING TYPE STRING. *----------------------------Logic-------------------------------------* "拼接字符串,两种语句效果相同 LV_STRING = LV_STR1 && LV_STR2. WRITE: / LV_STRING.
*--------------------------Variables-----------------------------------* DATA: LV_STR1 TYPE STRING VALUE 'Hello', LV_STR2 TYPE STRING VALUE 'World.', LV_STRING TYPE STRING, LV_TEST TYPE C VALUE ` `. *----------------------------Logic-------------------------------------* "添加拼接符 "上下两句效果相同,即便使用变量中间添加制表符空格也无法生效 LV_STRING = LV_STR1 && LV_TEST && LV_STR2. WRITE: / LV_STRING. LV_STRING = LV_STR1 && SPACE && LV_STR2. WRITE: / LV_STRING.
*--------------------------Variables-----------------------------------* DATA: LT_TABLE TYPE TABLE OF CHAR20, LV_STRING TYPE STRING. *----------------------------Logic-------------------------------------* "合并内表 APPEND 'Hello' TO LT_TABLE. APPEND 'World.' TO LT_TABLE. APPEND 'l am an' TO LT_TABLE. APPEND 'ABAP' TO LT_TABLE. APPEND 'programmer.' TO LT_TABLE.
CONCATENATE LINES OF LT_TABLE INTO LV_STRING. WRITE: / LV_STRING. "HelloWorld.l am anABAPprogrammer.
CONCATENATE LINES OF LT_TABLE INTO LV_STRING SEPARATED BY SPACE. WRITE: / LV_STRING. "Hello World. l am an ABAP programmer.
*--------------------------Variables-----------------------------------* DATA: LT_TABLE TYPE TABLE OF CHAR10. *----------------------------Logic-------------------------------------* APPEND 'Hello' TO LT_TABLE. APPEND 'World.' TO LT_TABLE. APPEND 'l am an' TO LT_TABLE. APPEND 'ABAP' TO LT_TABLE. APPEND 'programmer.' TO LT_TABLE. CONCATENATE LINES OF LT_TABLE INTO LV_STRING SEPARATED BY SPACE. WRITE: / LV_STRING. "Hello World. l am an ABAP programmer "保留尾部空格 CONCATENATE LINES OF LT_TABLE INTO LV_STRING RESPECTING BLANKS. WRITE: / LV_STRING. "Hello World. l am an ABAP programmer
SPLIT dobj AT sep INTO { {result1 result2 [...]} | {TABLE result_tab} } [IN {CHARACTER|BYTE} MODE].
3.语法解释与应用
(1)截取字符串到变量 INTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
*--------------------------Variables-----------------------------------* CONSTANTS: LC_SPLIT TYPE C LENGTH 1 VALUE '|'. DATA: LV_SPLIT TYPE STRING VALUE 'QWE|RTY|UIO', LV_STR1 TYPE STRING, LV_STR2 TYPE STRING, LV_STR3 TYPE STRING. *----------------------------Logic-------------------------------------* "截取字符串到变量 SPLIT LV_SPLIT AT LC_SPLIT INTO LV_STR1 LV_STR2 LV_STR3. WRITE: / LV_STR1, / LV_STR2, / LV_STR3. "运行结果: "QWE "RTY "UIO
(2)截取字符串到内表 TABLE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
*--------------------------Variables-----------------------------------* CONSTANTS: LC_SPLIT TYPE C LENGTH 1 VALUE '|'. DATA: LV_SPLIT TYPE STRING VALUE 'QWE|RTY|UIO', LT_TABSTR TYPE TABLE OF STRING. *----------------------------Logic-------------------------------------* "截取字符串到内表 SPLIT LV_SPLIT AT LC_SPLIT INTO TABLE LT_TABSTR. LOOP AT LT_TABSTR INTO LV_SPLIT. WRITE: / LV_SPLIT. CLEAR: LV_SPLIT. ENDLOOP. "运行结果: "QWE "RTY "UIO
十、字符串匹配模式
1.介绍
字符串的匹配模式主要包含以下四种:
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(是否包含图案)
注:在比较两个字符串时,字符串前的空格会计入进行比较,但是字符串末尾的空格不会计入进行比较。
2.包含于 / 不包含 字符比较 CO / CN
(1)说明
<str1> CO <str2>:str1包含于或等于str2,返回true。str1中字符均可以在str2中找到。区分大小写,顺序无要求。
*--------------------------Variables-----------------------------------* DATA: LV_STRING1 TYPE C LENGTH 3 VALUE 'qaz', LV_STRING2 TYPE C LENGTH 9 VALUE 'qweasdzxc', LV_STR1 TYPE C LENGTH 5 VALUE 'qaz', LV_STR2 TYPE C LENGTH 9 VALUE 'qweasdzxc'. *----------------------------Logic-------------------------------------* IF LV_STRING1 CO LV_STRING2. WRITE: / 'CO True'. ELSE. WRITE: / 'CO False'. ENDIF. "运行结果: "CO True IF LV_STR1 CO LV_STR2. WRITE: / 'CO True'. ELSE. WRITE: / 'CO False'. ENDIF. "运行结果: "CO False
*--------------------------Variables-----------------------------------* CONSTANTS: LC_NUMBER TYPE STRING VALUE '.0123456789'. DATA: LV_STRING1 TYPE STRING VALUE '123.45', LV_STRING2 TYPE STRING VALUE '123.45P'. *----------------------------Logic-------------------------------------* "判断字符串中是否全是数字 IF LV_STRING1 CO LC_NUMBER. WRITE: / '字符串中全是数值'. ELSE. WRITE: / '字符串中包含非数值字符'. ENDIF. "运行结果: "字符串中全是数值
*--------------------------Variables-----------------------------------* DATA: LV_STRING1 TYPE STRING VALUE 'QAZ', LV_STRING2 TYPE STRING VALUE 'qweasdzxc'. *----------------------------Logic-------------------------------------* IF LV_STRING1 NA LV_STRING2. WRITE: / 'NA True'. ELSE. WRITE: / 'NA False'. ENDIF. "运行结果: "NA True LV_STRING1 = 'QaZ'. LV_STRING2 = 'qweasdzxc'. IF LV_STRING1 NA LV_STRING2. WRITE: / 'NA True'. ELSE. WRITE: / 'NA False'. ENDIF. "运行结果: 两字符之间存在交集字符a "NA False
*--------------------------Variables-----------------------------------* DATA: LV_STRING1 TYPE STRING VALUE 'qweasdzxc', LV_STRING2 TYPE STRING VALUE '#q*', LV_STRING3 TYPE STRING VALUE '#Q*'. *----------------------------Logic-------------------------------------* IF LV_STRING1 CP LV_STRING2. WRITE: / 'CP True'. ELSE. WRITE: / 'CP False'. ENDIF. "运行结果: "CP True
*--------------------------Variables-----------------------------------* DATA: LV_STRING1 TYPE STRING VALUE 'qweasdzxc', LV_STRING2 TYPE STRING VALUE '#q*', LV_STRING3 TYPE STRING VALUE '#Q*'. *----------------------------Logic-------------------------------------* IF LV_STRING1 NP LV_STRING3. WRITE: / 'NP True'. ELSE. WRITE: / 'NP False'. ENDIF. "运行结果: "NP True