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

Comparison string with conditional statement

0 Kudos
3,183

Hi Experts,

Thanks in advance for you help.

i want to write a code using comparison string (CS, CA,NA,NS etc) with using case statement.

for ex- there is a filed lv_string which is having descriptions. so i want to validate some words in descriptions and want write code using CASE instead of IF- ELSE .

case lv_string.

when contains the letter 'text' or 'word'.

so how can i validate or write the code.

7 REPLIES 7
Read only

Former Member
0 Kudos
2,031

Hi Ujjwal, Any specific reason you want to use "CASE".?

Read only

0 Kudos
2,031

Hi Saransh,

Thank you for your response.

The reason behind using CASE is we can reduce no. of lines code.

please let me know if you any other efficient way to do it.

Regards

Ujjwal

Read only

0 Kudos
2,031

Hi Ujjwal,

CASE is better then IF-ELSE if there are 5 or more conditions.

But we cannot use (CS, CA,NA,NS) with CASE. If the words you want to validate are always at the fixed offset then you can always fetch those words (lv_string+x(y)) and then use CASE on that new word.

Thanks,

Saransh Walia.

Read only

0 Kudos
2,031

Hi,

Just to elaborate on the answer given by VenkatRamesh V.

Define ranges and get all the words that you want to compare in the low value of the range.

Loop at range.

if range-low cs lv_string.

*** Do your coding***

endif.

endloop.

Hope this helps.

Read only

VenkatRamesh_V
Active Contributor
0 Kudos
2,031

Hi,

Try

Declare   the Ranges.

Regards,

Ramesh

Read only

Chintu6august
Contributor
0 Kudos
2,031

Hi,

I dont think you can make use of case statement when you are comparing string as your dont know what other words will come in LV_STRING along with the words which you want to compare using case statement.

ABAP provide statements and classes for regular expression operation.

ABAP statement – REGEX ABAP statements

FIND and REPLACE support the use of regular expression with additional clause REGEX.

FIND statement searches the given text for a match with a regular expression, as shown below:

FIND REGEX 'text*word' IN LV_STRING.

FIND ALL OCCURRENCES OF REGEX pattern IN text

RESULTS match_result.

All the matching expression will be stored in match_result variables with type MATCH_RESULT_TAB.

thanks!!

Read only

former_member185116
Active Participant
0 Kudos
2,031

Hi ujjwal,

you can not use CS , CA ... with When inside CASE statement..

forget CASE satatement instead use IF..

regards,

vinay..