开发者

How can I loop through each character in a String using JSTL?

开发者 https://www.devze.com 2023-03-05 15:21 出处:网络
How can I loop 开发者_如何学JAVAthrough each character in a String using JSTL?Tricky use of fn:substring() would do

How can I loop 开发者_如何学JAVAthrough each character in a String using JSTL?


Tricky use of fn:substring() would do

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


Late to the party, but EL 2.2 allows for instance method calls (more on that here: https://stackoverflow.com/a/7122669/2047962). This means that you could shorten Jigar Joshi's answer by 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 is doing.


i think you can't do that with JSTL's forEach. You need to write your own tag or an EL function. Here is a sample code how you write your custom tags: http://www.java2s.com/Tutorial/Java/0360__JSP/CustomTagSupport.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消