You could use lastIndexOf() method on String

String last = string.substring(string.lastIndexOf('-') + 1);
Answer from Denis Bazhenov on Stack Overflow
Discussions

regex - Split string and get last element - Stack Overflow
Let's say I have a column which has values like: foo/bar chunky/bacon/flavor /baz/quz/qux/bax I.e. a variable number of strings separated by /. In another column I want to get the last element from More on stackoverflow.com
🌐 stackoverflow.com
How to split string, remove last element and join back in Java? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
java - How can I split a String in Expression Language and take the last element? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
Extract last element in a string array
I have an address field for which I am trying to extract the country during data preparation. For example: 123 West St, Washington, DC 20005, USA or 100 Main St STE 700, Office 754, Washington, DC 20004, USA Note that they don’t have the same number of elements in the address. More on community.amazonquicksight.com
🌐 community.amazonquicksight.com
1
0
January 26, 2024
🌐
Baeldung
baeldung.com › home › java › java string › get the last word of a string
Get the Last Word of a String | Baeldung
August 7, 2025 - In this short tutorial, we’ll learn how to get the last word of a String in Java using two approaches. The split() instance method from the String class splits the string based on the provided regular expression.
🌐
Coderanch
coderanch.com › t › 383339 › java › split-element
split and last element(s) (Java in General forum at Coderanch)
August 9, 2007 - If I use split, I get an array of 6 elements, as expected.however, if the final element in the input line is null, like this. I might have two different pipe delimited input formats, the only difference being that one format will have an extra field crammed into the middle of the string.so ...
🌐
Codemia
codemia.io › knowledge-hub › path › java_get_last_element_after_split
Java Get last element after split
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Quora
quora.com › What-is-the-simplest-way-to-get-the-last-two-word-of-a-string-in-Java
What is the simplest way to get the last two word of a string in Java? - Quora
Answer (1 of 3): You can use this: String string = “this is a string ”; String str[] = string.split(‘ ’); System.out.println(“last but one word is “ +str[(str.length-2)】+”last word is” +str[(str.length()-1)】; The former gives the last but one word and the latter the last word.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
String myStr = "Split a string ... myArray) { System.out.println(s); } ... The split() method splits a string into an array of substrings using a regular expression as the separator....
🌐
Pass4sure
pass4sure.com › blog › mastering-string-splitting-in-java-extracting-the-final-element-with-precision-and-performance
Mastering String Splitting in Java: Extracting the Final Element with Precision and Performance – IT Exams Training – Pass4Sure
If the input string lacks the delimiter entirely, Java’s split() method will return an array with one element — the original string itself. This scenario is common when users input data inconsistently. By checking whether the array contains only a single item, developers can infer that no delimiter was found. Still, the last item can be accessed normally and will simply mirror the original input.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Having the Last Word with Java and Groovy | InfoWorld
January 29, 2011 - Java String.split(String) on regular ... end of the provided String. Because String.split(String) returns an array, the last element of the array is accessed with array syntax whether in Java or in Groovy....
🌐
Amazon QuickSight
community.amazonquicksight.com › q&a
Extract last element in a string array - Q&A - Amazon Quick Community
January 26, 2024 - I have an address field for which I am trying to extract the country during data preparation. For example: 123 West St, Washington, DC 20005, USA or 100 Main St STE 700, Office 754, Washington, DC 20004, USA Note that they don’t have the same number of elements in the address.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-java-string-split-last-occurrence
Java String Split Last Occurrence: A Comprehensive Guide - CodingTechRoom
In this tutorial, we will delve into how to split a string in Java at its last occurrence of a specified delimiter. This technique is vital in many applications, particularly where the last part of a string holds special significance.
🌐
Intellipaat
intellipaat.com › home › blog › java: get last element after split
Java: Get last element after split - Intellipaat
February 3, 2026 - In Java, the split() method is used for dividing a string into an array of substrings on the basis of a delimiter. Now, if you are required to retrieve the last element from the split result, you can do so by using simple array operations.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
2 weeks ago - If n is zero then the pattern will ... for example, yields the following results with these parameters: An invocation of this method of the form str.split(regex, n) yields the same result as the expression...
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - String[] splitted = input.trim().split("\\s*,\\s*"); Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter. We can achieve the same result by using Java 8 Stream features: