二维码

[基础语法] abap中数字字母(alpha numeric)的合法性检查

Twilight发表于 2014-12-09 10:38Twilight 最后回复于 2014-12-09 10:38 [复制链接] 5759 0

1、数字字母(alpha numeric)的合法性检查
程序代码:
  1. * Declare the variable
  2. * For Length
  3. DATA: serial_length TYPE i.
  4. * For Alpha numeric
  5. DATA: str TYPE string.
  6. DATA: valid_characters TYPE string.
  7. * Fill in those valid characters you need to check
  8. CONCATENATE '0123456789' 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' INTO valid_characters.
  9. *CONSTANTS:valid_characters TYPE string VALUE '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.
  10. * User Input
  11. PARAMETERS testchar(10) DEFAULT '1234005abc'.
  12. * Get User Input
  13. str = testchar.
  14. * The Checks
  15. IF str CO valid_characters.
  16.   WRITE: / str, 'characters are ok'.
  17. ELSE.
  18.   WRITE: / str, 'characters are not ok'.
  19. ENDIF.
  20. *– End Program
复制代码

程序执行效果:
alpha numeric.jpg

2、去掉一个字符串中的所有特殊字符
程序代码:
  1. DATA: l_teststr TYPE string,
  2. l_alpha_numeric TYPE string.
  3. DATA: l_strlen TYPE i.
  4. DATA: l_char TYPE c.
  5. l_teststr = 'ref0 0@2$45*8'.
  6. l_strlen = STRLEN( l_teststr ).
  7. WRITE: / l_teststr, 'length:', l_strlen.
  8. l_alpha_numeric = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.
  9. WHILE l_strlen <> 0.
  10.   l_strlen = l_strlen - 1.
  11.   l_char = l_teststr+l_strlen(1).
  12.   IF l_alpha_numeric NA l_char.
  13.     REPLACE SECTION OFFSET l_strlen LENGTH 1 OF l_teststr WITH ''.
  14.   ENDIF.
  15. ENDWHILE.
  16. l_strlen = STRLEN( l_teststr ).
  17. WRITE: / l_teststr, 'length:', l_strlen.
复制代码

程序执行效果:
alpha numeric 2.jpg
ps:这里的校验库字符串和需校验的字符串都必须为string类型,C类型也不行

相关拓展:abap比较字符串和数字串
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表