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).
Asked
Active
Viewed 1.6e+01k times
59
-
related: http://stackoverflow.com/questions/1181969/java-get-last-element-after-split – GreenMatt Jan 12 '11 at 19:03
-
3Title says "fastest", questions asks for "simplest". Please clarify. – Mark Bolusmjak Jan 12 '11 at 20:42
-
1Fastest and simplest for coding and reading. Execution time is irrelevant. As an aside, having different wording is better for search purposes although the discrepancy was unintentional. – Muhd Jan 12 '11 at 21:49
7 Answers
178
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);

OscarRyz
- 196,001
- 113
- 385
- 569
-
11
-
One line is nice since I will be using it in Velocity template code. Could be combined with a regular expession to account for other characters like JST's solution. – Muhd Jan 12 '11 at 19:26
-
2Actually this is better, it already accounts for a single word: String lastWord = test.substring(test.lastIndexOf(" ")+1); – Muhd Jan 12 '11 at 19:56
-
1what if a sentence has some punctuation at the end? "This is a sentence?!" – Zippp Jul 30 '17 at 23:12
22
String testString = "This is a sentence";
String[] parts = testString.split(" ");
String lastWord = parts[parts.length - 1];
System.out.println(lastWord); // "sentence"

Dave McClelland
- 3,385
- 1
- 29
- 44
-
This is the solution I probably would have come up with on my own, not bad, but a one-liner would be better. – Muhd Jan 12 '11 at 19:05
-
When you say "fastest," do you mean shortest in code or fastest to execute? This could be made into a one liner, but it wouldn't execute any faster and would be much harder to read. – Dave McClelland Jan 12 '11 at 19:08
-
1I did not say "fastest", I said "simplest", by which I meant a combination of brevity and ease of understanding. – Muhd Jan 12 '11 at 19:10
-
2@Muhd If it were me, I would think the above is easiest to read. Then again, this is also the solution I immediately came up with. @OscarRyz's solution is also simple to read, so it comes down to personal preference. – Dave McClelland Jan 12 '11 at 19:14
-
@Dave Probably mine, is not easier to read anymore for I have added a check in case the string it self is the last word. – OscarRyz Jan 12 '11 at 19:22
-
Yeah, now Dave's is easiest to read, although one can cure hard to read code with a quick comment. – Muhd Jan 12 '11 at 19:28
-
14
Here is a way to do it using String
's built-in regex capabilities:
String lastWord = sentence.replaceAll("^.*?(\\w+)\\W*$", "$1");
The idea is to match the whole string from ^
to $
, capture the last sequence of \w+
in a capturing group 1, and replace the whole sentence with it using $1
.

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523
-
1
-
-
Edge case: This fails in right-to-left languages (ex. Hebrew). @OscarRyz substring method still works. https://ideone.com/MacH0s – CeePlusPlus Nov 17 '22 at 18:57
12
If other whitespace characters are possible, then you'd want:
testString.split("\\s+");

JST
- 1,154
- 6
- 15
5
You can do that with StringUtils
(from Apache Commons Lang). It avoids index-magic, so it's easier to understand. Unfortunately substringAfterLast
returns empty string when there is no separator in the input string so we need the if
statement for that case.
public static String getLastWord(String input) {
String wordSeparator = " ";
boolean inputIsOnlyOneWord = !StringUtils.contains(input, wordSeparator);
if (inputIsOnlyOneWord) {
return input;
}
return StringUtils.substringAfterLast(input, wordSeparator);
}

palacsint
- 28,416
- 10
- 82
- 109
0
String s="print last word";
x:for(int i=s.length()-1;i>=0;i--) {
if(s.charAt(i)==' ') {
for(int j=i+1;j<s.length();j++) {
System.out.print(s.charAt(j));
}
break x;
}
}

Sapan Nn
- 1
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 17:02