Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly split strings in JSTL?

Tags:

split

jsp

jstl

How can I split strings separated by "/" inside a jsp page using JSTL?

I have a string in this format: **

"23/11/2010"

*. Sometimes, the string could be like this: *

"1/1/2010"

*. I need to do something in order to split the string in three different substrings: *

"23", "11", "2010".

** This is because I need to put each one of them inside three different text fields, like these:

<input type="text" value="23">/
<input type="text" value="11">/
<input type="text" value="2010">

I could not find any working example yet.

Thanks in advance!

like image 291
Lucas Avatar asked Apr 24 '12 18:04

Lucas


People also ask

Which tag in JSTL is used to iterate over a string by splitting with supplied delimiters?

JSTL forTokens tag is another tag in core JSTL library to support Iteration or looping. It effectively compliments, more useful <c:forEach> tag, by allowing you to iterate over comma-separated or any delimited String. You can use this tag to split string in JSP and can operate on them individually.

What is the difference between JSP and JSTL?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

How do I find the length of a string in JSTL?

JSTL - fn:length() Function The fn:length() function returns the string length or the number of items in a collection.


2 Answers

You can use the fn:split() function for this.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:set var="dateParts" value="${fn:split(dateString, '/')}" />
...
<input type="text" name="day" value="${dateParts[0]}" />
<input type="text" name="month" value="${dateParts[1]}" />
<input type="text" name="year" value="${dateParts[2]}" />

Be sure that the date format is validated beforehand :) It would be easier if it was a java.util.Date, not a java.lang.String. You could then use <fmt:formatDate> to format it to a reliable and fixed string format first. Otherwise you'd need to add checks on array length by fn:length() and to prevent potential XSS attack holes by fn:escapeXml().

Also important to note is that the function takes a regular expression as argument and not just a plain character sequence. So in case you'd like to split on characters which represent special characters in regex, then you'd need to escape them with backslashes. See also How to split a string in Java for general guidelines which also apply to fn:split().

like image 71
BalusC Avatar answered Sep 20 '22 14:09

BalusC


It's worth noting for anyone else who finds this question in their searching (as I did) that JSTL has the useful tag <c:forTokens>. This will split the input string by a supplied delimiter and then iterate over the resultant collection of tokens.

As demonstrated here, the following code:

<c:forTokens items="A,B,C,D" delims="," var="mySplit">
   <c:out value="${mySplit}"/></br>
</c:forTokens>

Would result in:

A
B
C
D

Documentation: JSTL core Tag forTokens

like image 30
Luke Avatar answered Sep 21 '22 14:09

Luke