Can I figure out lastIndexOf()
function at JSTL or JSP ?
I just found the method int indexOf(java.lang.String, java.lang.String)
in JSTL. Or has there anyway to work this ?
can try :
<c:set value="${fn:split('fooBar/BarFoo/Bar','/')}" var="separatorPosition" />
<c:out value="${separatorPosition[fn:length(separatorPosition)-1]}">
</c:out>
How about using fn:split
and sum lengths (fn:length
) of all components excluding the last one.
But it's much better to do all logic in backend, just add additional properties to your object.
Here's one that seems to work for me. Modify myStr and lastIndexOf to what you want.
<!-- Prepare variables -->
<c:set var="myStr" value="this_is my. string_ok"/>
<c:set var="lastIndexOf" value="_"/>
<c:set var="lastIndexFoundAt" value=""/>
<c:set var="myStrSize" value="${fn:length(myStr) }"/>
<c:set var="indexIn" value="0"/>
<c:set var="indexOut" value="1"/>
<!-- iterate through string -->
<c:forEach var="i" begin="0" end="${myStrSize}">
<c:if test="${indexOut <= myStrSize }">
<c:set var="char" value="${fn:substring(myStr, indexIn, indexOut) }"/>
<c:if test="${char == lastIndexOf }">
<c:set var="lastIndexFoundAt" value="${i}"/>
</c:if>
<c:set var="indexIn" value="${indexIn + 1 }"/> <!-- increment -->
<c:set var="indexOut" value="${indexOut + 1 }"/> <!-- increment -->
</c:if>
</c:forEach>
to test the index, use
<c:out value="${lastIndexFoundAt}/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With