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("."));
Answer from Eran on Stack OverflowUse 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
How to get the string before .(dot) and after /(last) slash in Java - Stack Overflow
Any simpler way to get the last element of a Java array? - Stack Overflow
split - How to substring a string to the before last dot(.) in java? - Stack Overflow
java - How can I split a String in Expression Language and take the last element? - Stack Overflow
You 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
Suppose that you have that string saved in a variable named myString.
String myString = "core/pages/viewemployee.jsff";
String newString = myString.substring(myString.lastIndexOf("/")+1, myString.indexOf("."));
But you need to make the same control before doing substring in this one, because if there aren't those characters you will get a "-1" from lastIndexOf(), or indexOf(), and it will break your substring invocation.
I suggest looking for the Javadoc documentation.
You can solve this with regex (given you only need a group of word characters between the last "/" and "."):
String str="core/pages/viewemployee.jsff";
str=str.replaceFirst(".*/(\\w+).*","$1");
System.out.println(str); //prints viewemployee
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.
Use the String method lastIndexOf(int ch).
int lastIndxDot = st.lastIndexOf('.');
st.substring(0, lastIndxDot); will be the substring you want. If it returned -1, then there is no '.' in the string.
EDIT:
for (Map.Entry < String, String > entry: newList.entrySet()) {
String getAfterDot = entry.getKey();
int lastIndxDot = getAfterDot.lastIndexOf('.');
if (lastIndxDot != -1) {
String beforeDot = getAfterDot.substring(0, lastIndxDot);
System.out.println(beforeDot);
}
}
Try this code it should work:
static HashMap < String, String > newList = new HashMap < > ();
public static void main(String[] args) throws FileNotFoundException, IOException {
String inputFile = "input";
BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
String line = null;
while ((line = brInput.readLine()) != null) {
newList.put(line, "x");
}
for (Map.Entry < String, String > entry: newList.entrySet()) {
String getAfterDot = entry.getKey();
if (getAfterDot.contains(".")) {
String[] split = getAfterDot.split("\\.");
String beforeDot = "";
beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
System.out.println(beforeDot);
}
}
}
I got this output with above code:
0
0
2
0.3.5
0.2
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, ".")];
The file extension is the part after the last dot and therefore the following should work
String str = "Udemy.txt";
String fileName = str.substring(0, str.lastIndexOf("."));
String extension = str.substring(str.lastIndexOf(".") + 1);
Try this,
String fileName = "MyFile.xls";
int dotIndex = fileName.lastIndexOf(".");
String name = fileName.substring(0, dotIndex); // MyFile
String extension = fileName.substring(dotIndex); // .xls
// then you can change and re-construct the file name
Try substring with lastIndexOf method of String:
String str = "almas.test.tst";
System.out.println(str.substring(str.lastIndexOf(".") + 1));
Output:
tst
I think you can use lastIndexOf(String str) method for this purpose.
String str = "test.test.test....test";
int pos = str.lastIndexOf("test");
String result = str.substring(pos);