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: 

Searching for a whole word within a sentence

former_member498918
Participant
0 Kudos

I am trying to search for a whole word only in a sentence. The problem is if I use CS then I get things that contain the string in other words. If I use CO or CP then there is no match because there are other words in the sentence. I don't think it's practical to split the sentence into individual words and then compare each word as the sentence is of an unknown length and could be made up of many words.

An example:

Sentence 1 - THIS IS ABOUT RF OUTPUTS.

Sentence 2 - THIS IS ABOUT MY PERFORMANCE

The user wants to search for sentences containing RF. In this search I only want sentence 1 to be found and not the RF in the word performance in sentence 2.

Can anyone tell me how to do this.

Many thanks

Karen

1 ACCEPTED SOLUTION

former_member181962
Active Contributor
0 Kudos

Include even the spaces in the comparison:

data: v_str1 type string value 'THIS IS ABOUT RF OUTPUTS',
 v_str2 type string value 'THIS IS ABOUT MY PERFORMANCE'.

 if v_str1 cs ' RF '.
write:/ 'Suc'.
else.
write:/ 'Not suc'.
 endif.

 if v_str2 cs ' RF '.
write:/ 'Suc'.
else.
write:/ 'Not suc'.
 endif.

Regards,

Ravi

6 REPLIES 6

former_member181962
Active Contributor
0 Kudos

Include even the spaces in the comparison:

data: v_str1 type string value 'THIS IS ABOUT RF OUTPUTS',
 v_str2 type string value 'THIS IS ABOUT MY PERFORMANCE'.

 if v_str1 cs ' RF '.
write:/ 'Suc'.
else.
write:/ 'Not suc'.
 endif.

 if v_str2 cs ' RF '.
write:/ 'Suc'.
else.
write:/ 'Not suc'.
 endif.

Regards,

Ravi

Former Member
0 Kudos

use serach command.

DATA: string1(30) TYPE c VALUE 'This is about RF OUTPUT.'.

SEARCH string1 FOR 'RF' .

Former Member
0 Kudos
REPORT YCHATEST.

DATA : V_STR(500) VALUE 'THIS IS ABOUT RF OUTPUTS'.

SEARCH V_STR FOR ' RF '.

IF SY-SUBRC EQ 0.
  WRITE : / 'String found'.
ELSE.
  WRITE : / 'String not found'.
ENDIF.

Former Member
0 Kudos

Oh sorry , ravi already given the answer

raymond_giuseppi
Active Contributor
0 Kudos

You may search for ' RF ' with leading and trailing space, but there will be errors when RF is first or last word of text.

you could try something like

* First put the searched text between space
  clear w_sentence. " Where w_sentence is at least 2 char longer than sentence
  w_sentence+1 = sentence.
* Then put the searched text between dot and space (look at search syntax)
  concatenate '.' word '.' into w_word separated by space. " so 'RF' become '. RF .'
* Now we can check
  SEARCH w_sentence for w_word . " The dot ensures space are not ignored
  IF SY-SUBRC = 0
* sy-fdpos is already correct

Regards

naveen1241
Participant
0 Kudos

DATA: str1(50),

str2(50),

srchstr(10).

DATA: BEGIN OF st_str,

str(50),

END OF st_str.

DATA: it_tab LIKE TABLE OF st_str WITH HEADER LINE.

str1 = 'THIS IS ABOUT RF OUTPUTS RF'.

str2 = 'THIS IS ABOUT MY PERFORMANCE RF'.

srchstr = 'RF'.

SPLIT str1 AT space INTO TABLE it_tab.

LOOP AT it_tab WHERE str = srchstr.

WRITE : srchstr.

WRITE 'found in'.

WRITE 'str1'.

EXIT.

ENDLOOP.

CLEAR it_tab[].

SPLIT str2 AT space INTO TABLE it_tab.

LOOP AT it_tab WHERE str = srchstr.

WRITE : srchstr.

WRITE 'found in'.

WRITE 'str2'.

EXIT.

ENDLOOP.

Check this code as it solves the leading RF and trailing RF problem .