2007 Apr 12 2:30 PM
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
2007 Apr 12 2:34 PM
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
2007 Apr 12 2:34 PM
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
2007 Apr 12 2:35 PM
use serach command.
DATA: string1(30) TYPE c VALUE 'This is about RF OUTPUT.'.
SEARCH string1 FOR 'RF' .
2007 Apr 12 2:38 PM
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.
2007 Apr 12 2:40 PM
2007 Apr 12 2:45 PM
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
2007 Apr 12 3:21 PM
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 .