How can I scroll through each character in a string using JSTL?

How can I scroll each character in a String using JSTL?

+3
source share
3 answers

Overuse fn:substring()will do

<c:forEach var="i" begin="0" end="${fn:length(str)}" step="1">
    <c:out value="${fn:substring(str, i, i + 1)}" />     
</c:forEach>
+11
source

Late to the party, but EL 2.2 allows you to call method calls (more on this here: fooobar.com/questions/139030 / ... ). This means that you can shorten Jigar Joshi's answer with a few characters:

<c:forEach var="i" begin="0" end="${fn:length(str)}" step="1">
  <c:out value="${str.charAt(i)}" />     
</c:forEach>

I only suggest this because it is a little more obvious what your code does.

+1
source

I think you cannot do this with JSTL forEach. You need to write your own tag or EL function. Here is a sample code of how you write your own tags: http://www.java2s.com/Tutorial/Java/0360__JSP/CustomTagSupport.htm

0
source

All Articles