Application Development and Automation 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: 
Read only

how to code this ..please help

Former Member
0 Likes
839

Hi Friends:

I've a variable whose value is like 'XX-5011'. Now I've a requirement. I've to put a validation like this....

if this begins with 'XX'.

do something.

else.

do something.

what should be the best way of putting this check that whether it starts from XX or not.

suitable points will be rewarded.

Regards:

Gaurav

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
814

hi

ex: var1= 'XX-5011'.

if var1+0(2) = 'XX'.

<do the processing>

else.

<do the processing>

endif.

regards,

madhu

6 REPLIES 6
Read only

Former Member
0 Likes
815

hi

ex: var1= 'XX-5011'.

if var1+0(2) = 'XX'.

<do the processing>

else.

<do the processing>

endif.

regards,

madhu

Read only

George_Lioumis
Active Participant
0 Likes
814

Hi.

Just write:

condense variable.

if variable+0(2) eq 'XX'.

...do what you need....

else.

... do something else....

endif.

Regards,

George

Read only

prasanth_kasturi
Active Contributor
0 Likes
814

hi

its better if you use an if condition

if var+0(2) = 'XX'.

<code>.

else.

<code>.

endif.

regards

prasanth

Read only

Former Member
0 Likes
814

Hi

say var1= 'XX-5011'.

if var1+0(2) = 'XX'.

else.

endif.

regards,

Sandesh

Reward points

Read only

matt
Active Contributor
0 Likes
814

Generally, it is BAD to use offsets if you can avoid it. So while the above will work, they are not good practice. Good practice would be:

IF this CP 'XX*'.

Note, however, that with CP, upper/lower case is disregarded. If you need to consider case use

DATA: moff TYPE i.
FIND 'XX' IN this RESPECTING CASE MATCH OFFSET moff.
IF sy-subrc IS NOT INITIAL or moff GT 0.
  WRITE: / 'Fail'.
ELSE.
  WRITE: / 'Sucess'.
ENDIF.

Or, if you want to be super cool.


FIND REGEX '^XX.*' IN this.
IF sy-subrc IS NOT INITIAL.
  WRITE: / 'Fail'.
ELSE.
  WRITE: / 'Sucess'.
ENDIF.

Edited by: Matthew Billingham on May 13, 2008 2:14 PM

OK - never mind. I see you chose to follow bad practice.

Read only

Former Member
0 Likes
814

hi,

you can use *(star), +(plus),#(hash) along with CP(contain pattern).

plz see below details also.

The CP (contains pattern) and NP (no pattern) operators perform a string search that allows pattern-matching characters. The expression v1 CP v2 is true when v1 contains a sting that matches the pattern in v2. The expression v1 NP v2 is true when v1 does not contain a string that matches the pattern in v2. The pattern matching characters allowed in v2 are given below.

Character Used to

*(STAR) Match any sequence of characters

+(PLUS) Match any single character

#(HASH) Interpret the next character literally

#is the escape character. A single character following it is interpreted exactly. Special meaning, if it exists, is lost. You can also use # to make a search case sensitive or to search for the *, +, or # character. Below table shows examples of how you might use these characters. The escape character is needed when you want to perform a case-sensitive search using CS, NS, CP, or NP. You also need it if you want to perform a pattern search (CP or NP) for a string containing *, +, or #.

Statement True When

v1 CP ‘A+C’(A plus C) v1 contains “a” in the first position and “c” in the third. Either character can be in upper- or lowercase. Any

character can appear in the second position

v1 CP ‘Ab’(star AB star) The string can appear anywhere within v1. Either character can be in upper- or lowercase.

v1 CP ‘#A#b’( star hash A hash B star) v1 contains a capital A followed by lowercase b

v1 CP ‘##’( star hash hash star) v1 contains a #

i think this is useful for u,

*reward me if it is useful to u.*

Edited by: pradeep kandgal on May 13, 2008 2:18 PM