ABAP Help Document(10):3.3预定义Function
3.3预定义Function
1.Logic Functions
示例:
"预定义Function FORM f_pre_functions_logic. "调用function "语法:func( val ) "语法:func( param1 = val1 param2 = val2 ……) "1.Logical Functions "布尔function "boolc, boolx "boolc( log_exp ),返回string "boolx( bool = log_exp bit = bit ),返回xstring DATA:lv_bool TYPE string. lv_bool = boolc( lv_bool IS INITIAL ). WRITE:/ lv_bool. "包含function "contains, contains_any_of, contains_any_not_of "语法: "1.contains( val = text sub|start|end = substring [case = case] "[off = off] [len = len] [occ = occ] ) "2.contains( val = text regex = regex [case = case] "[off = off] [len = len] [occ = occ] ) "3.contains_any_of( val = text sub|start|end = substring "[off = off] [len = len] [occ = occ] ) "4.contains_any_not_of( val = text sub|start|end = substring "[off = off] [len = len] [occ = occ] ) "case:是否大小写敏感;off:匹配开始偏移量;len:偏移量后长度字符串匹配;occ:匹配次数,默认1 DATA:lv_con1 TYPE string. DATA:lv_con2 TYPE string. lv_con1 = 'hello world'. lv_con2 = 'wo'. "contains,相当于CS;not contains,相当于NS "contains_any_of,相当于CA;not contians_any_of,相当于NA "contains_any_not_of,相当于CN,not contians_any_not_of,相当于CO IF contains( val = lv_con1 sub = lv_con2 case = 'X' off = 0 len = 5 occ = 1 ). WRITE:/ 'test contains,true'. ELSE. WRITE:/ 'test contains,false'. ENDIF. IF contains( val = lv_con1 sub = lv_con2 case = 'X' off = 0 len = strlen( lv_con1 ) occ = 1 ). WRITE:/ 'test contains,true'. ELSE. WRITE:/ 'test contains,false'. ENDIF. "匹配function "matches "语法: "matches( val = text regex = regex [case = case] "[off = off] [len = len] ) "match( val = text regex = regex [case = case] [occ = occ] ) IF matches( val = lv_con1 regex = '^h.*' case = 'X' off = 0 len = strlen( lv_con1 ) ). WRITE:/ 'test match,true'. ELSE. WRITE:/ 'test match,false'. ENDIF. "match返回匹配子串 lv_con2 = match( val = lv_con1 regex = '^h.{0,5}' case = 'X' ). WRITE:/ lv_con2. ENDFORM.
2.Number Functions
示例:
FORM f_pre_functions_number. "2.Numerical Functions "abs, ceil, floor, frac, sign, trunc "abs绝对值 DATA:lv_abs1 TYPE I VALUE -20. DATA:lv_abs2 TYPE F VALUE '-2.333'. lv_abs1 = abs( lv_abs1 ). lv_abs2 = abs( lv_abs2 ). WRITE:/ lv_abs1,lv_abs2. "ceil不小于arg的最小整数 "floor不大于arg的最大整数 DATA:lv_cf1 TYPE F VALUE '3.21'. DATA:lv_cf2 TYPE I. lv_cf2 = ceil( lv_cf1 ). WRITE:/ lv_cf2. lv_cf2 = floor( lv_cf1 ). WRITE:/ lv_cf2. "frac小数部分 "trunc整数部份 DATA:lv_ft TYPE F VALUE '3.992'. DATA:lv_frac TYPE F. DATA:lv_trunc TYPE I. lv_frac = frac( lv_ft ). lv_trunc = trunc( lv_ft ). WRITE:/ lv_frac,lv_trunc. "nmax, nmin "语法:nmax|nmin( val1 = arg1 val2 = arg2 [val3 = arg3] ... [val9 = arg9] ) DATA:lv_val TYPE I. DATA:lv_val1 TYPE F VALUE '3.22'. DATA:lv_val2 TYPE I VALUE 1. DATA:lv_val3 TYPE P LENGTH 8 DECIMALS 2 VALUE '4.22'. DATA:lv_val4 TYPE DECFLOAT16 VALUE '4.67'. DATA:lv_val5 TYPE DECFLOAT34 VALUE '5.33'. lv_val = nmax( val1 = lv_val1 val2 = lv_val2 val3 = lv_val3 val4 = lv_val4 val5 = lv_val5 ). WRITE:/ lv_val. lv_val = nmin( val1 = lv_val1 val2 = lv_val2 val3 = lv_val3 val4 = lv_val4 val5 = lv_val5 ). WRITE:/ lv_val. "acos, asin, atan, cos, sin, tan, cosh, sinh, tanh, exp, log, log10, sqrt "反三角函数,三角函数,双曲三角函数,自然对数,开方 "round, rescale 四舍五入 "语法: "round( val = arg {dec = n}|{prec = n} [mode = m] ) "dec 指定小数字数I,prec 指定精度I,mode 模式 "CL_ABAP_MATH类中模式 *ROUND_HALF_UP 如果中间值,向上舍入, *ROUND_HALF_DOWN 如果中间值,向下舍入 *ROUND_HALF_EVEN 四舍五入,最后一位小数为偶数 *ROUND_UP 向上舍入 *ROUND_DOWN 向下舍入 *ROUND_CEILING *ROUND_FLOOR DATA:lv_round TYPE DECFLOAT34. DATA:lv_round1 TYPE F VALUE '3.284'. "返回3.28 lv_round = round( val = lv_round1 dec = 2 mode = CL_ABAP_MATH=>ROUND_HALF_UP ). WRITE:/ lv_round. "3 lv_round = round( val = lv_round1 prec = 1 mode = CL_ABAP_MATH=>ROUND_HALF_DOWN ). WRITE:/ lv_round. "3.28 lv_round = round( val = lv_round1 dec = 2 mode = CL_ABAP_MATH=>ROUND_HALF_EVEN ). WRITE:/ lv_round. "3.29 lv_round = round( val = lv_round1 dec = 2 mode = CL_ABAP_MATH=>ROUND_UP ). WRITE:/ lv_round. "语法: "rescale( val = arg {dec = n}|{prec = n} [mode = m] ) "更改浮点数小数点位置,返回decfloat34 DATA:lv_scale TYPE DECFLOAT34. DATA:lv_scale1 TYPE F VALUE '12345.6789'. lv_scale = rescale( val = lv_scale1 dec = 2 mode = CL_ABAP_MATH=>ROUND_DOWN ). WRITE:/ lv_scale. lv_scale = rescale( val = lv_scale1 prec = 6 mode = CL_ABAP_MATH=>ROUND_DOWN ). WRITE:/ lv_scale. ENDFORM.
3. String Functions
示例:
"String Functions FORM f_pre_functions_string. "charlen, dbmaxlen, numofchar, strlen "字符串长度 DATA:lv_str TYPE string VALUE 'hello world'. DATA:lv_str1 TYPE C LENGTH 10 VALUE '你好'. DATA:lv_len TYPE I. "返回第一个字符长度,1字节字符返回1,占2字节字符返回2 lv_len = charlen( lv_str ). WRITE:/ lv_len. lv_len = charlen( lv_str1 ). WRITE:/ lv_len. "返回string类型最大长度,sstring,rawstring "Type Group:abap中,abap_max_db_string_ln or abap_max_db_rawstring_ln "se11->type group中查看 lv_len = dbmaxlen( lv_str ). WRITE:/ lv_len. "返回字符串长度 "区别: "non-Unicode,a character that uses two bytes is only counted once. lv_len = numofchar( lv_str ). WRITE:/ lv_len. "non-Unicode,a character that uses two bytes is only counted twice. lv_len = strlen( lv_str ). WRITE:/ lv_len. "char_off "语法:char_off( val = text add = pos [off = off] ) "返回指定偏移量1相距pos字符位置 lv_len = char_off( val = lv_str add = 2 off = 1 ). WRITE:/ lv_len. "cmax, cmin "语法:cmax|cmin( val1 = text1 val2 = text2 [val3 = text3] ... [val9 = text9] ) "返回字符串比较最大最小值 DATA:lv_text1 TYPE C LENGTH 10 VALUE 'ABC'. DATA:lv_text2 TYPE N LENGTH 10 VALUE '113'. DATA:lv_text3 TYPE string VALUE 'hello'. DATA:lv_text4 TYPE string VALUE '您好'. DATA:lv_text TYPE string. lv_text = cmax( val1 = lv_text1 val2 = lv_text2 val3 = lv_text3 val4 = lv_text4 ). WRITE:/ lv_text. lv_text = cmin( val1 = lv_text1 val2 = lv_text2 val3 = lv_text3 val4 = lv_text4 ). WRITE:/ lv_text. "count, count_any_of, count_any_not_of "语法:count( val = text {sub = substring}|{regex = regex} [case = case] "[off = off] [len = len] ) "语法:count_any_of( val = text sub = substring "[off = off] [len = len] ) "语法:count_any_not_of( val = text sub = substring "[off = off] [len = len] ) "统计子字符串出现次数 DATA:lv_count TYPE string VALUE 'hello world,welcome'. DATA:lv_count1 TYPE string VALUE 'he'. DATA:lv_count_num TYPE I. "子串整体比较 lv_count_num = count( val = lv_count sub = lv_count1 case = 'X' off = 0 len = strlen( lv_count ) ). WRITE:/ lv_count_num. "每个字符单个统计 lv_count_num = count_any_of( val = lv_count sub = lv_count1 off = 0 len = strlen( lv_count ) ). WRITE:/ lv_count_num. "每个字符单个不匹配统计 lv_count_num = count_any_not_of( val = lv_count sub = lv_count1 off = 0 len = strlen( lv_count ) ). WRITE:/ lv_count_num. "distance "语法:distance( val1 = text1 val2 = text2 [max = max] ) "返回两个字符串Levenshtein distance "编辑距离,两个字符串通过添加,删除,移动变成相同字符串 "编辑距离越小,越相似 DATA:lv_dis TYPE string VALUE 'if'. DATA:lv_dis1 TYPE string VALUE 'iff'. DATA:lv_dis_num TYPE I. "设置max,当编辑距离大于max,返回max lv_dis_num = distance( val1 = lv_dis val2 = lv_dis1 ). WRITE:/ lv_dis_num. "condense "语法:condense( [val =] text [del = del] [from = from] [to = to] ) "去掉开始、结尾del中指定字符 DATA:lv_cond TYPE string. DATA:lv_cond1 TYPE string VALUE 'xxxwelcomexxx'. "返回elcome lv_cond = condense( val = lv_cond1 del = 'xw' ). WRITE:/ lv_cond. "将from字符串替换为to指定字符串 lv_cond = condense( val = lv_cond1 del = 'x' from = 'wel' to = '' ). WRITE:/ lv_cond. "concat_lines_of "语法:concat_lines_of( [table =] itab [sep = sep] ) TYPES:c80 TYPE C LENGTH 80. DATA:lt_c TYPE TABLE OF c80. DATA:ls_c LIKE LINE OF lt_c. DATA:lv_concat TYPE string. ls_c = 'hello'. APPEND ls_c TO lt_c. ls_c = 'world'. APPEND ls_c TO lt_c. lv_concat = concat_lines_of( table = lt_c sep = '&' ). WRITE:/ lv_concat. "escape "语法:escape( val = text format = format ) "format: CL_ABAP_FORMAT类中"E_xxx" "在不同格式化语言中,特殊字符转换 "XML,HTML,JS,URL等 DATA:lv_escape TYPE string. DATA:lv_escape1 TYPE string VALUE '& < > " '' TAB LF CR BS FF \ ctrl-char '. lv_escape = escape( val = lv_escape1 format = CL_ABAP_FORMAT=>E_XML_TEXT ). WRITE:/ lv_escape. lv_escape = escape( val = lv_escape1 format = CL_ABAP_FORMAT=>E_XML_ATTR ). WRITE:/ lv_escape. lv_escape = escape( val = lv_escape1 format = CL_ABAP_FORMAT=>E_URL ). WRITE:/ lv_escape. "find, find_end, find_any_of, find_any_not_of "语法:find( val = text {sub = substring}|{regex = regex} [case = case] "[off = off] [len = len] [occ = occ] ) "find_end( val = text regex = regex [case = case] "[off = off] [len = len] [occ = occ] ) "find_any_of( val = text sub = substring "[off = off] [len = len] [occ = occ] ) "find_any_not_of( val = text sub = substring "[off = off] [len = len] [occ = occ] ) DATA:lv_find TYPE string VALUE 'hello world,welcome'. DATA:lv_find1 TYPE string VALUE 'wo'. DATA:lv_find_num TYPE I. "查找指定子串,返回子串开始所在偏移量 "case 是否大小写敏感,off 匹配开始位置 len 比较长度,occ 匹配次数 "occ正数,从左到右;occ负数,从右到左 lv_find_num = find( val = lv_find sub = lv_find1 case = 'X' off = 0 len = strlen( lv_find ) occ = 1 ). WRITE:/ lv_find_num. "查找指定子串,返回子串结束所在偏移量 lv_find_num = find_end( val = lv_find sub = lv_find1 case = 'X' off = 0 len = strlen( lv_find ) occ = 1 ). WRITE:/ lv_find_num. "大小写敏感 "返回属于子串字符的字符在字符串位置 lv_find_num = find_any_of( val = lv_find sub = lv_find1 off = 0 len = strlen( lv_find ) occ = 1 ). WRITE:/ lv_find_num. "返回不属于子串字符的字符在字符串位置 lv_find_num = find_any_not_of( val = lv_find sub = lv_find1 off = 0 len = strlen( lv_find ) occ = 1 ). WRITE:/ lv_find_num. "insert "语法:insert( val = text sub = substring [off = off] ) "插入子字符串 DATA:lv_insert TYPE string VALUE 'hello'. DATA:lv_insert1 TYPE string VALUE 'head:'. lv_insert = insert( val = lv_insert sub = lv_insert1 off = 0 ). "head:hello WRITE:/ lv_insert. "match "match( val = text regex = regex [case = case] [occ = occ] ) "返回匹配正则表达式的字符子串 "repeat "语法:repeat( val = text occ = occ ) "返回重复字符串 DATA:lv_repeat TYPE string. DATA:lv_repeat1 TYPE string VALUE 'hello'. lv_repeat = repeat( val = lv_repeat1 occ = 5 ). WRITE:/ lv_repeat. "replace "语法:replace( val = text [off = off] [len = len] with = new ) "语法:replace( val = text {sub = substring}|{regex = regex} "with = new [case = case] [occ = occ] ) "字符串替换 DATA:lv_replace TYPE string VALUE 'hello,world,welcome'. DATA:lv_replace1 TYPE string VALUE 'hi'. lv_replace = replace( val = lv_replace off = 0 len = 5 with = lv_replace1 ). WRITE:/ lv_replace. lv_replace = replace( val = lv_replace sub = 'welcome' with = 'nice job' ). WRITE:/ lv_replace. "reverse "reverse( [val =] text ) "字符串逆转 DATA:lv_reverse TYPE string VALUE 'abc'. lv_reverse = reverse( val = lv_reverse ). WRITE:/ lv_reverse. "segment "语法:segment( val = text index = idx [sep|space = sep] ) "使用sep分割字符串,返回指定部分分割子串 DATA:lv_segment TYPE string VALUE 'AB;CD;EF'. "分割成AB CD EF三部分,返回CD第二部分 lv_segment = segment( val = lv_segment index = 2 sep = ';' ). WRITE:/ lv_segment. "shift_left, shift_right "语法:shift_left( [val =] text "[places = places]|[circular = places]|[sub = substring] ) "shift_right( [val =] text "[places = places]|[circular = places]|[sub = substring] ) "可以只有val一个参数,将左边或右边空格移除 DATA:lv_shift TYPE string. lv_shift = ' well '. lv_shift = shift_left( lv_shift ). WRITE:/ lv_shift. "places lv_shift = ' good day '. lv_shift = shift_left( val = lv_shift places = 5 ). WRITE:/ lv_shift. "circular,循环 lv_shift = ' good day '. lv_shift = shift_left( val = lv_shift circular = 5 ). WRITE:/ lv_shift. "sub,左边或右边所有与子串匹配字符串左移 lv_shift = ' good day '. lv_shift = shift_left( val = lv_shift sub = ' good' ). WRITE:/ lv_shift. "substring, substring_after, substring_from, substring_before, substring_to "语法: "substring( val = text [off = off] [len = len] ) "substring_from( val = text {sub = substring}|{regex = regex} "[case = case] [occ = occ] [len = len] ) "substring_after( val = text {sub = substring}|{regex = regex} "[case = case] [occ = occ] [len = len] ) "substring_before( val = text {sub = substring}|{regex = regex} "[case = case] [occ = occ] [len = len] ) "substring_to( val = text {sub = substring}|{regex = regex} "[case = case] [occ = occ] [len = len] ) DATA:lv_substring TYPE string. DATA:lv_substring1 TYPE string VALUE 'test,hello,world'. lv_substring = substring( val = lv_substring1 off = 5 len = 5 ). WRITE:/ lv_substring. "substring_from,case,大小写敏感;sub 匹配子串; occ 第几次匹配; len,返回字符串长度 "返回ello 第二个e匹配后字符串,长度为4,包含e lv_substring = substring_from( val = lv_substring1 sub = 'e' occ = 2 len = 4 ). WRITE:/ lv_substring. "返回llo, 第二个e匹配后字符串,长度为4,不包含e lv_substring = substring_after( val = lv_substring1 sub = 'e' occ = 2 len = 4 ). WRITE:/ lv_substring. "返回st,h 第二个e匹配之前字符串,长度为4,不包含e lv_substring = substring_before( val = lv_substring1 sub = 'e' occ = 2 len = 4 ). WRITE:/ lv_substring. "返回t,he 第二个e匹配之前字符串,长度为4,包含e lv_substring = substring_to( val = lv_substring1 sub = 'e' occ = 2 len = 4 ). WRITE:/ lv_substring. "to_upper, to_lower, to_mixed, from_mixed "语法: "to_upper( [val =] text ) "to_lower( [val =] text ) "to_mixed( [val =] text [sep = sep] [case = case] [min = min] ) "from_mixed( [val =] text [sep = sep] [case = case] [min = min] ) DATA:lv_upper TYPE string VALUE 'ABcdefg'. DATA:lv_lower TYPE string VALUE 'ABedFG你'. "字符转换为大小写 lv_upper = to_upper( lv_upper ). lv_lower = to_lower( lv_lower ). WRITE:/ lv_upper,lv_lower. "奇怪的函数 DATA:lv_mixed TYPE string VALUE 'my home well'. "将首字母大写,将sep指定字符删除,将后一字符大写 lv_mixed = to_mixed( val = lv_mixed sep = 'O' case = 'X' min = 2 ). WRITE:/ lv_mixed. lv_mixed = 'MY HOME well'. "mOy OhOoOmOe well lv_mixed = from_mixed( val = lv_mixed sep = 'O' case = 'x' min = 1 ). WRITE:/ lv_mixed. "translate "语法:translate( val = text from = from to = to ) "将text中所有from子串,替换成to子串 DATA:lv_trans TYPE string. lv_trans = '----!hello!---'. lv_trans = translate( val = lv_trans from = '-' to = '' ). WRITE:/ lv_trans. ENDFORM.
4.Byte Functions
示例:
FORM f_pre_functions_byte. "xstrlen "语法:xstrlen( arg ) "返回字节长度 DATA:lv_xstr TYPE xstring VALUE 'FF00EE'. DATA:lv_len TYPE I. lv_len = xstrlen( lv_xstr ). WRITE:/ lv_len. "bit-set "语法:bit-set( arg ) "返回xstring "1->80 2->40 3->20 4->10 5->08 6->04 ,怎么转换的? lv_xstr = bit-set( 6 ). WRITE:/ lv_xstr. ENDFORM.
5.Table Functions
示例:
"table functions FORM f_pre_functions_table. "非常常用 "语法:lines( arg ) "内表行数 DATA:lt_tab TYPE TABLE OF spfli. DATA:lv_line TYPE I. SELECT * INTO TABLE lt_tab FROM spfli. lv_line = lines( lt_tab ). WRITE:/ lv_line. ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/14686996.html