Search code examples
javasearchcpu-wordlocate

Method of locating a part of a word


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.


Solution

  • 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);
    }