Java: Simplest way to get last word in a string
What is the simplest way to get the last word of a string in Java? You can assume no punctuation (just alphabetic characters and whitespace).
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
String testString = "This is a sentence";
String[] parts = testString.split(" ");
String lastWord = parts[parts.length - 1];
System.out.println(lastWord); // "sentence"