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: How do I check if the character belongs to one of the characters?

Former Member
0 Kudos

I have a string. I want to know if the 1st characters of the string belongs to one of the character set or not.

For example, the character set is A, B, E, Z, T.

Right now, what I did was:


 IF str+0(1) EQ 'A' OR str+0(1) EQ 'B' OR
        str+0(1) EQ 'E' OR str+0(1) EQ 'Z' OR ......

Is there a shorter way to do it?

Please help and I will reward you.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Use CA operator

data : string1 type TABLE OF string WITH HEADER LINE.

APPEND 'ABCDEF' to string1.
APPEND 'BBCDEF' to string1.
APPEND 'CBCDEF' to string1.
APPEND 'DBCDEF' to string1.
APPEND 'EBCDEF' to string1.

LOOP AT string1.
  check string1+0(1) CA 'ABE'.
  write: / sy-tabix,'found'.
ENDLOOP.

Cheers,

jose.

3 REPLIES 3

Former Member
0 Kudos

Hi,

Use CA operator

data : string1 type TABLE OF string WITH HEADER LINE.

APPEND 'ABCDEF' to string1.
APPEND 'BBCDEF' to string1.
APPEND 'CBCDEF' to string1.
APPEND 'DBCDEF' to string1.
APPEND 'EBCDEF' to string1.

LOOP AT string1.
  check string1+0(1) CA 'ABE'.
  write: / sy-tabix,'found'.
ENDLOOP.

Cheers,

jose.

Former Member
0 Kudos

Hi,

Try

 str+0(1) CA 'ABEZT'.

CA Contains Any:

True, if operand1 contains at least one character from operand2. Upper/lower case and trailing blanks are taken into account for both operands. If operand1 or operand2 is of type string and initial, the logical expression is always false. If result of the comparison is positive, sy-fdpos contains the offset of the first character in operand1 that is also contained in operand2. If the result of the comparison is negative, sy-fdpos contains the length of operand1.

Hope this helps.

Thanks,

Balaji

Former Member
0 Kudos

Hi,

say your charector set is

v_vharset = 'A, B, E, Z, T'.

your string is

v_string = 'Elephant'.

1. IF v_string+0(1) CA 'A,B,E,Z,T' .

ELSE.

ENDIF.

2.

IF v_string+0(1) IN 'A,B,E,Z,T' .

ELSE.

ENDIF.