Magpie

private int findKeyword(String statement, String goal, int startPos) { String phrase = statement.trim(); // The only change to incorporate the startPos is in the line below int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos); // Refinement--make sure the goal isn't part of a word while (psn >= 0) { // Find the string of length 1 before and after the word String before = " ", after = " "; if (psn > 0) { before = phrase.substring (psn - 1, psn).toLowerCase(); } if (psn + goal.length() < phrase.length()) { after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase(); } // If before and after aren't letters, we've found the word if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0)) // before is not a letter && ((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0))) { return psn; } // The last position didn't work, so let's find the next, if there is one. psn = phrase.indexOf(goal.toLowerCase(), psn + 1); } return -1; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.