2

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

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • How can you get value from text array using String as a index value? – Surendheran Jun 13 '14 at 09:21
  • @SurendarKannan I am not using String as a index value? – Koray Tugay Jun 13 '14 at 09:22
  • Then this means text[myStringHere]? – Surendheran Jun 13 '14 at 09:30
  • text[] is a function I am calling, it is really not important for the question. – Koray Tugay Jun 13 '14 at 09:35
  • Fine if it's a function, it will be like this text(myStringHere).. – Surendheran Jun 13 '14 at 13:47
  • @SurendarKannan There is nothing wrong with that part. Please see: http://stackoverflow.com/questions/7079978/how-to-create-a-custom-el-function Find on this page: Finally you can use it as intended: I do not know why you are so obssessed about that part of the question, it is not even relavent? – Koray Tugay Jun 13 '14 at 14:08
  • EL was introduced to clean up JSP pages and avoid scriptlets (so that programming logic is moved out of the JSPs). IMHO its better to implement this logic in the server-side. Any constraint on implementing the logic on server-side and returning the result? – Teddy Jun 16 '14 at 15:18

3 Answers3

6

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

Braj
  • 46,415
  • 5
  • 60
  • 76
0

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, ".")];
Kabulan0lak
  • 2,116
  • 1
  • 19
  • 34
0

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

Community
  • 1
  • 1
tonystrawberry
  • 102
  • 1
  • 2
  • 12