Reputation: 23800
This is what I am trying but it seems not to work:
(myStringHere.split(".")[(myStringHere.split(".").length)-1]).concat(text[myStringHere])
The string I have will be something like this:
com.foo.bar.zar.gar.ThePartIWant
ThePartIWant is what I want to show in the page only.
I am using Expression Language 2.2
Upvotes: 3
Views: 12016
Reputation: 46861
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
Upvotes: 7
Reputation:
Try this :
String s = "com.foo.bar.zar.gar.ThePartIWant";
System.out.println(s.split("\\.")[(s.split("\\.").length)-1]);
"." is a special character. You have to escape it because period means any character in regex.
Source : How to split a string in Java
Upvotes: 0
Reputation: 2136
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, ".")];
Upvotes: 0