Business requirment经常要求某个ID的值只能包含26个字母和10个数字字符,这就要求ABAP程序能够检查相应变量内容,辨别特殊字符。sap-img上的一个例子提供了这样的功能:

REPORT ZCHECK_ALPHA_NUMERIC.

* Declare the variable

* For Length
data: serial_length type i.

* For Alpha numeric
data: str type string.
data: valid_characters type string.

* Fill in those valid characters you need to check
concatenate ‘0123456789′ ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
‘abcdefghijklmnopqrstuvwxyz’ into valid_characters.

* User Input
parameters testchar(10) default ‘12345abc’.

* Get User Input
str = testchar.

* The Checks
if str co valid_characters.
write: / str, ‘Characters are OK’.
else.
write: / str, ‘Characters are NOT OK’.
endif.

*– End Program

基于同样的原理,下面这个subroutine可以去掉一个字符串中的所有特殊字符

*&———————————————————————*
*&      Form  convert_character_entities
*&———————————————————————*
*  This form removes all non-alpha-numeric characters from a string
*———————————————————————-*
*      <–
*———————————————————————-*

FORM remove_non_alpha_numeric_char.
DATA: l_teststr       TYPE string,
l_alpha_numeric TYPE string.
DATA: l_strlen TYPE i.

DATA: l_char TYPE c.

l_teststr = ‘ref0 0@2$45*8′.
l_strlen = strlen( l_teststr ).
WRITE: / l_teststr, ‘length:’, l_strlen.

l_alpha_numeric = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’.

WHILE l_strlen <> 0.
l_strlen = l_strlen – 1.
l_char = l_teststr+l_strlen(1).
IF l_alpha_numeric NA l_char.
REPLACE SECTION OFFSET l_strlen LENGTH 1 OF l_teststr WITH ”.
ENDIF.
ENDWHILE.
l_strlen = strlen( l_teststr ).
WRITE: / l_teststr, ‘length:’, l_strlen.

ENDFORM.

posted on 2009-08-03 19:45  levin  阅读(1904)  评论(0编辑  收藏  举报