Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

string functions to check if string contains charactets other than digits

Former Member
0 Kudos

We have a string as an input.

We need to check if it contains any characters other than digits ( ie. 0123456789) The other characters could be special characters, albhabets etc.

Please suggest any RFC / Logic to get this .

Rohit

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
IF string CO '0123456789'.
==> only digits
ELSE.
==> not just digits (could be space as well)
ENDIF.
8 REPLIES 8

Former Member
0 Kudos

if lv_str CA '0123456789'

else.

endif.

use CA option in ur case..

vijay

JozsefSzikszai
Active Contributor
IF string CO '0123456789'.
==> only digits
ELSE.
==> not just digits (could be space as well)
ENDIF.

0 Kudos

Suppose string = '1234ABCD'.

Then your logic will give results that the string contains digits/

My requirement is that we have to raise an error if the string contains any thing other than digits.

Please help

0 Kudos
string  =   '1234ABCD'.

if string ca '0123456789 '
write 'ok' 
else.
message ' string contains non numeric fields. 
endif.

so trigger the message in the else part .

0 Kudos

Eric's solution (CO) works just fine, sniper's (CA) is wrong, at least for your requirement here.

CO = "contains only", CA = "contains any".

Thomas

P.S. you could also use CN "contains not only" and switch the IF/ELSE-parts around, if you prefer...

0 Kudos

It returns that the string contain only digits..please check

if you know the solution better, than why ask at all?

if you get different replies, why don't you try them and see if works or not?

CA stands for contains any

IF string CA '0123456789'

it will be true if the string contains at least one digit

IF string CA '0123456789'
==> the string contains at least one digit
ELSE.
==> the string does NOT contain any digit (however could contain space or special characters)
ENDIF.

CO stands for contains only

IF string CO '0123456789'

it will be true if the string contains only digits

IF string CO '0123456789'
==> the string contains only digits
ELSE.
==> the string contains not only digits (could be no digits at all)
ENDIF.

==> the string contains only

</Spoonfeeding>

PS: Thomas: Thanks for your support / Vielen Dank für die Unterstützung!

Former Member
0 Kudos

hi,

compare that string with system field sy-abcde..

ex:

data : var type string.

var = '84748291'.
if var CA sy-abcde.
  write : 'Hello'.
else.
  write : 'NO'.
endif.
.

Rgds.,

subash