Oninez
Oninez

Reputation: 15

How to find occurrence of a string in another string where it's not embedded into another word?

I am trying to create a static method "indexOfKeyword" and return an indexOf a string where the string is not embedded into another word. It would return -1 if there's no such occurrence.

For example,

String s = "In Florida, snowshoes generate no interest.";
String keyword = "no";

This would return 31.

the only problem I believe is not being to find the next occurrence of the string keyword. What I have so far is:

public static int indexOfKeyword( String s, String keyword )
{

s = s.toLowerCase();
keyword = keyword.toLowerCase();


int startIdx = s.indexOf( keyword );



while ( startIdx >= 0 )
{

String before = " ";
String after = " ";

if ( startIdx > 0 ){

     before = s.substring( startIdx - 1 , startIdx);
 }


int endIdx = startIdx;


 if ( endIdx < s.length() ){

     after = s.substring( startIdx + keyword.length() , startIdx + keyword.length() + 1);
 }


if ( !(before.compareTo("a") >= 0 && before.compareTo("z") <= 0 && after.compareTo("a") >= 0 &&     after.compareTo("z") <= 0)){
  return startIdx;
}




startIdx = 
   /* expression using 2-input indexOf for the start of the next occurrence */

}


  return -1;
}


 public static void main( String[] args )  
 { 
 // ... and test it here 
String s = ""; 
String keyword = ""; 

System.out.println( indexOfKeyword( s, keyword ) ); 
} 

Upvotes: 0

Views: 793

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

Something like this:

String input = "In Florida, snowshoes generate no interest.";
String pattern = "\\bno\\b";
Matcher matcher = Pattern.compile(pattern).matcher(input);

return matcher.find() ? matcher.start() : -1;

Strings not embedded in another word are not necessarily delimited by spaces. It could be comma's, periods, the beginning of the string, etc.

The above solution uses regular expression word boundaries (\b) to give a correct solution.


If there's a risk of your keyword containing characters that have a special meaning when used in regular expressions, you probably want to escape it first:

String pattern = "\\b" + Pattern.quote(keyword) + "\\b";

So a complete method implementation could look like this:

public static int indexOfKeyword(String s, String keyword) {
    String pattern = "\\b" + Pattern.quote(keyword) + "\\b";
    Matcher matcher = Pattern.compile(pattern).matcher(s);

    return matcher.find() ? matcher.start() : -1;
}

Upvotes: 2

Related Questions