You could use lastIndexOf() method on String
String last = string.substring(string.lastIndexOf('-') + 1);
Answer from Denis Bazhenov on Stack OverflowYou could use lastIndexOf() method on String
String last = string.substring(string.lastIndexOf('-') + 1);
Save the array in a local variable and use the array's length field to find its length. Subtract one to account for it being 0-based:
String[] bits = one.split("-");
String lastOne = bits[bits.length-1];
Caveat emptor: if the original string is composed of only the separator, for example "-" or "---", bits.length will be 0 and this will throw an ArrayIndexOutOfBoundsException. Example: https://onlinegdb.com/r1M-TJkZ8
Another way to get the last token/word
String lastToken = sentance.replaceAll(".* ", "");
You only need to split it once, and take the last element.
String sentence = "Any simpler way to get the last element of a Java array?";
String[] tokens = sentence.split(" ");
String lastToken = tokens[tokens.length-1];
It's awkward, but there's really no other way to do it unless you have foreknowledge of the length of the string.
regex - Split string and get last element - Stack Overflow
How to split string, remove last element and join back in Java? - Stack Overflow
java - How can I split a String in Expression Language and take the last element? - Stack Overflow
Extract last element in a string array
Edit: this one is simplier:
=REGEXEXTRACT(A1,"[^/]+$")
You could use this formula:
=REGEXEXTRACT(A1,"(?:.*/)(.*)$")
And also possible to use it as ArrayFormula:
=ARRAYFORMULA(REGEXEXTRACT(A1:A3,"(?:.*/)(.*)$"))
Here's some more info:
- the RegExExtract function
- Some good examples of syntax
- my personal list of Regex Tricks
This formula will do the same:
=INDEX(SPLIT(A1,"/"),LEN(A1)-len(SUBSTITUTE(A1,"/","")))
But it takes A1 three times, which is not prefferable.
You could do this too
=index(SPLIT(A1, "/"), COLUMNS(SPLIT(A1, "/"))-1)
How about .substring() & .lastIndexOf()?
String file = filesFound[0];
String newFileName = file.substring(0, file.lastIndexOf("_"));
the newFileName would then contain everything up to (but not including) the last '_' char.
Arrays.copyOf returns a new array, so you have to assign it to t or a new variable:
t = Arrays.copyOf(t, t.length - 1)
If you are doing it in JSP then try with JSP JSTL function tag library that provide lost of methods as defined here in JavaDoc
Read more here on Oacle The Java EE 5 Tutorial - JSTL Functions
Here is the code to get the last value based on split on dot.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<c:set var="string1" value="This.is.first.String." />
<c:set var="string2" value="${fn:split(string1, '.')}" />
<c:set var="lastString" value="${string2[fn:length(string2)-1]}" />
<c:out value="${lastString }"></c:out>
output:
String
Here is one more example
You want to catch only the last part of your string ? Try this :
string[] s_tab = myStringHere.split(".");
string result = s_tab[s_tab.length - 1];
If you want it in one line :
string result = myStringHere.split(".")[StringUtils.countMatches(myStringHere, ".")];
How about:
String numbers = text.substring(text.length() - 7);
That assumes that there are 7 characters at the end, of course. It will throw an exception if you pass it "12345". You could address that this way:
String numbers = text.substring(Math.max(0, text.length() - 7));
or
String numbers = text.length() <= 7 ? text : text.substring(text.length() - 7);
Note that this still isn't doing any validation that the resulting string contains numbers - and it will still throw an exception if text is null.
Lots of things you could do.
s.substring(s.lastIndexOf(':') + 1);
will get everything after the last colon.
s.substring(s.lastIndexOf(' ') + 1);
everything after the last space.
String numbers[] = s.split("[^0-9]+");
splits off all sequences of digits; the last element of the numbers array is probably what you want.
Use myString.lastIndexOf(".") to get the index of the last dot.
For example, if you are sure that your input String contains at least one dot :
String name = myString.substring(0,myString.lastIndexOf("."));
If you want to use split, you need to escape the dot (split expects a regular expression).
List<String> strings = Arrays.asList(myString.split("\\."));
If you only need to remove the last part, you can use replaceAll and a regex:
myString = myString.replaceAll("\\.[^.]*$", "");
Explanation:
\\.looks for a dot[^.]*looks for 0 or more non dot characters$is the end of the string