
1. Overview
In this article, we will learn the various ways to get the last word of a Java String.
2. Use String#substring method to get the last word
The substring() method of the java String
class returns a part of the string. You can use the below overloaded substring
method to get the last word of a Java string.
public String substring(int startIndex)
Now, we know the method to get a part of the string. But how to find the startIndex
of the last word of a string.
You must look for the index of the last space
character in the string and next to it would be your last word. To do this, use the lastIndexOf
method.
pubic int lastIndexOf(int ch)
Let’s take the below code. The index of space
is 26 and last word sentence
starts from 27th index.
public class Main { public static void main(String args[]) { String lastWordStr = "Find the last word in this sentence".trim(); String lastWord = lastWordStr.substring(lastWordStr.lastIndexOf(" ")+1); System.out.println(lastWord); } }
The above code would work correctly even if the string contains only one word. For example, the below lastWordStr
has only a single word sentence
. Still, this substring
expression would return the sentence
correctly.
Here, the lastIndexOf
returns -1 as there is no space
found in the string and next index to -1 is 0. So, the substring
method takes the string from index 0 and returns it.
public static void main(String args[]) { String lastWordStr = "sentence"; String lastWord = lastWordStr.substring(lastWordStr.lastIndexOf(" ")+1); System.out.println(lastWord); }
2.2. Use String#split method to get string last word
You can use split
method of the String
class to split the string using the space
delimiter into array of words.
In the below code, we have split the lastWordStr
by using the space
. So parts
array would have the elements “Last”, “word”, “in”, “a”, “sentence”. Now, we get the last element or word "sentence"
from the array using its index.
public static void main(String args[]) { String lastWordStr = "Last word in a sentence"; String[] parts = lastWordStr.split(" "); String lastWord = parts[parts.length - 1]; System.out.println(lastWord); }
2.3. Use String#replaceAll method to get the last word of a string
You can use replaceAll
method of String to get the last word of the string.
In the below code, replaceAll
regex match the entire string from ^
(start) to $
(end). Then, captures the last sequence of \w+
in a capturing group 1 (captures the last word sentence
), and replaces the entire string using $1
(capturing group).
public static void main(String args[]) { String lastWordStr = "Last word in a sentence"; String lastWord = lastWordStr.replaceAll("^.*?(\\w+)\\W*$", "$1"); System.out.println(lastWord); }
.*
? matches as few characters possible (except for line terminators)- 1st Capturing Group (\w+)\w matches any word character ([a-zA-Z0-9_])+ as many times possible
- \W matches any non-word character ([^a-zA-Z0-9_])* as many times as possible
2.4. Use StringUtils#substringAfterLast apache commons
You can use substringAfterLast
method of StringUtils
class that is available in apache commons library. To use it, you must add the apache commons dependency in your project.
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency>
In the below code, we are checking if the string has only a single word or not. If there are more words, then we invoke the substringAfterLast
by passing the space
as an argument to separate the last word.
public static void main(String args[]) { String input = "Last word in a sentence"; getLastWord(input); } public static String getLastWord(String input) { String lastWord; String wordSeparator = " "; boolean singleWordStr = !StringUtils.contains(input, wordSeparator); if (singleWordStr) { lastWord = input; } lastWord = StringUtils.substringAfterLast(input, wordSeparator); System.out.println(lastWord); return lastWord; }
3. Conclusion
To sum up, we have seen the various ways to get the last word of a Java String.