0

I'm fairly new to Java programming and I'm wondering if there is any method of locating a specific set of characters in the last word of a sentence.

An example of this would be trying to find the characters "go" in the phrase : I'm am going to the Go-station.

Looking at this, we can see that the characters "go" appears twice but is there any method of locating just the "go" in the last word of the phrase ("Go-station") and not the first one.

IM-MC
  • 53
  • 5
  • 1
    Yes. The method is to write your own method. Break the problem into pieces. 1) Separate the sentence into words. 2) Look for your string in the last word. – Gilbert Le Blanc Jan 29 '16 at 01:57

2 Answers2

2

I think this works:

String phrase = "I'm am going to the Go-station.";
String[] words = phrase.split(" ");
int relIndex = words[words.length - 1].toLowerCase().indexOf("go");

if (relIndex == -1) {
    System.out.println("Not found");
} else {
    int index = phrase.length() - words[words.length - 1].length() + relIndex;
    System.out.println("Index: " + index);
}

It's easy to understand, but there is probably a simpler way.

I'm breaking up the phrase into individual words then checking the last word for the index of "go". Then using the relative index of "go" in the last word of the phrase I calculate the index of "go" in the original phrase.

Zarwan
  • 5,537
  • 4
  • 30
  • 48
1

EDIT: this method lastIndexOf(String str) worked by coincidence. @Zar's answer is correct. Let me write my own corrected version here as this answer is already accepted.

String[] words = "I'm am going to the Go-station.".split(" ");
int index = words[words.length - 1].toLowerCase().indexOf("go");

if (index == -1) {
System.out.println("Not found");
} else {
int result = "I'm am going to the Go-station.".length() - words[words.length - 1].length() + index;
System.out.println("found it at postion: " + result);
}
Faraz
  • 6,025
  • 5
  • 31
  • 88
  • 3
    No, it won't. You have mixed cases, and no guarantee that the lastIndexOf is in the last word. – Gilbert Le Blanc Jan 29 '16 at 01:58
  • @Khatarnaak Gunda your answer is incorrect. Don't forget to do "toLowerCase" before; otherwise you're int result will be faulty – adhg Jan 29 '16 at 02:00
  • @Gilbert Le Blanc : Oh. I couldnt try it myself. I just went to String documentation and found this method. Thanks – Faraz Jan 29 '16 at 02:00
  • @adhg : give me 1 sec – Faraz Jan 29 '16 at 02:01
  • Forgot the dot: `"I'm am going to the Go-station.".lastIndexOf("go"); ` – ruyili Jan 29 '16 at 02:01
  • 1
    This answer is still wrong. Refer to the comment @GilbertLeBlanc made. – Zarwan Jan 29 '16 at 03:29
  • @Zar I think it is correct. It will return the last occurrence of "go". I don't see any problem with it. You tell me specifically what is the problem with the code? – Faraz Jan 29 '16 at 04:28
  • Read the question. You don't want the last occurance of go. You want the occurance of go in the last word of the phrase. – Zarwan Jan 29 '16 at 12:46
  • It works for his example by coincidence, not the general problem. It will fail for the example "Go see the apple tree" where the answer is that "go" does not appear in the last word at all. – Zarwan Jan 29 '16 at 13:28
  • @Zar you are correct. My answer is wrong. What do I do now? – Faraz Jan 29 '16 at 20:14
  • Correct it or delete it? I don't think you can delete it while it's marked as accepted though. – Zarwan Jan 29 '16 at 20:17