Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a String in Expression Language and take the last element?

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

like image 513
Koray Tugay Avatar asked Nov 21 '25 03:11

Koray Tugay


1 Answers

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

like image 101
Braj Avatar answered Nov 22 '25 15:11

Braj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!