You could use lastIndexOf() method on String

String last = string.substring(string.lastIndexOf('-') + 1);
Answer from Denis Bazhenov on Stack Overflow
🌐
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
This article unpacks the various ... string is divided into multiple substrings based on a delimiter. In Java, this is handled by the built-in split() method which returns an array of substrings....
🌐
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
🌐
Medium
medium.com › @AlexanderObregon › javas-string-split-method-explained-77bdaddaae79
Java’s String split() Method Explained
June 25, 2024 - In this example, the string is empty, and the split() method is called with a comma , as the delimiter. The output will be: ... Even though the string is empty, the split() method returns an array with one element (the empty string itself).
🌐
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.
🌐
Denodo Community
community.denodo.com › answers › question › details
Split a string and return the last element as a string (not an array)
Hi There, I have a string in the following format: /section1/section2/section3 I need to return only **section3** as a string. Is there a way to do this? Regards, Adam
🌐
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 ...
Find elsewhere
🌐
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.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Having the Last Word with Java and Groovy | InfoWorld
January 29, 2011 - The Java example that uses String.split(String) on a more limited basis (supplying ” ” to the method rather than s+ works the same way but splits on only single space (” “) rather than on any whitespace character. Groovy’s GDK String class provides a convenience String.split() method that ...
🌐
CodeGym
codegym.cc › java blog › strings in java › java string split() method
Java String split method
December 5, 2024 - The first step is to split the string using ";" as the delimiter in order to break it into its component parts. Then each resulting substring will hold information about a separate product, which we can process later.
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
Java Examples Java Videos Java ... myArray) { System.out.println(s); } ... The split() method splits a string into an array of substrings using a regular expression as the separator....
🌐
How to do in Java
howtodoinjava.com › home › string › java string split() : splitting by one or multiple delimiters
Java String split() : Splitting by One or Multiple Delimiters
October 12, 2023 - String str = "how-to-do.in.java"; String[] strArray1 = str.split("-"); //[how, to, do.in.java] - 3 tokens String[] strArray2 = str.split("-|\\."); //[how, to, do, in, java] - 5 tokens · The split() method is overloaded and accepts the following ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - The split() method in Java is used to divide a string into multiple parts based on a specified delimiter (regular expression). It returns an array of substrings after splitting the original string.
🌐
Intellipaat
intellipaat.com › home › blog › java: get last element after split
Java: Get last element after split - Intellipaat
February 3, 2026 - If the string ends with the delimiter, split() will ignore the trailing empty element unless you use negative limit in split(). String userInput = "red,pink,green,"; String[] parts = userInput.split(",", -1); System.out.println(parts[parts.length ...
🌐
Java2Blog
java2blog.com › home › bash › bash string › bash split string and get last element
Bash Split String and Get Last Element [9 Ways] - Java2Blog
April 16, 2023 - Here, the -d option specifies the space character as a delimiter, and the -t parameter removes any trailing newlines. Next, the last element of a string is extracted from the words array using ${words[-1]}.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | split string and get last element
Python | Split String and Get Last Element - Be on the Right Side of Change
December 7, 2022 - # Input: text = 'Java_C++_C#_G... Output: Split String: ['Java_C++_C#_Golang', 'Python'] Last Element: Python · Unlike the previous example, the input string ends with the separator itself. However, the output is similar. So, you have a different input string, but you have to generate a similar output by eliminating ...
🌐
Java2Blog
java2blog.com › powershell-split-and-get-last-value
PowerShell Split and Get Last Value [4 Ways]
April 24, 2023 - This domain name has expired. If you are the registered holder of this name and wish to renew it, please contact your registration service provider · java2blog.com · Related Search Topics · Copyright © java2blog.com. All rights reserved.Privacy Policy