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

Help with a string search

Former Member
0 Likes
785

Ok, I need syntax when I am merging tables.

I need to search a particular field and do some logic against it.

For example, field A1 could say "i have no idea how to code this problem" and I would have to search for a BEGINNING phrase "i have no idea..."

If found, A1 = B1 (a different field).

Could someone post syntax on how to search for a phrase within a field?

Would you use the search?

If so, then what would the if statement look like?

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
760

You can search it with SEARCH command,

Like this:

REPORT  ZTEST_NP.

data: l_text type string,
      l_find type string.


l_text = 'i have no idea how to code this problem'.
l_find = 'i have no idea'.

search l_text for l_find.
if sy-subrc = 0.
 write: 'found'.
** do your processing.

endif.

Regards,

Naimesh Patel

6 REPLIES 6
Read only

naimesh_patel
Active Contributor
0 Likes
761

You can search it with SEARCH command,

Like this:

REPORT  ZTEST_NP.

data: l_text type string,
      l_find type string.


l_text = 'i have no idea how to code this problem'.
l_find = 'i have no idea'.

search l_text for l_find.
if sy-subrc = 0.
 write: 'found'.
** do your processing.

endif.

Regards,

Naimesh Patel

Read only

0 Likes
760

Hey Naimesh,

If I have multiple phrases to search for, such as if the field contains "this phrase" then do this, if the field contains "this other phrase" then do this, if the field contains "this guy" then do this.

What would the statement look like?

Would you use CASE? Would you just use SEARCH....IF....ENDIF...repeat???

Thanks

Read only

0 Likes
760

Yes I would suggest to make in loop rather doing hardcode:

try this piece of code:

REPORT  ZTEST_NP.

DATA: L_TEXT TYPE STRING.

DATA: BEGIN OF IT_FIND OCCURS 0,
      FIND TYPE STRING,
      END   OF IT_FIND.

L_TEXT = 'i have no idea how to code this problem'.
IT_FIND-FIND = 'problem'.
APPEND IT_FIND.

IT_FIND-FIND = 'i have no idea'.
APPEND IT_FIND.

LOOP AT IT_FIND.
  SEARCH L_TEXT FOR IT_FIND-FIND.
  IF SY-SUBRC = 0.
    WRITE: / IT_FIND-FIND,'was found'.
** do your processing.

  ENDIF.
ENDLOOP.

Regards,

Naimesh Patel

Read only

0 Likes
760

Points to Naimesh. Thanks man. Too bad we dont work together- it would be sweet learning what you know.

Read only

0 Likes
760

Don't worry.. Someday we will ...

Regards,

Naimesh Patel

Read only

Former Member
0 Likes
760

Hi

You have to use the String commands like

CP (Contains pattern)

CS(contains string) etc for this purpose

data : str (30) type c,

str1(15) type c.

str = 'i have no idea how to code this problem'.

str1 = 'i have no idea'.

if str CP str1.

....do something...

endif.

if str CS str1.

....do something...

endif.

Regards

Anji