0

When I am trying to split the string based on "--"(double hyphen) its also splitting the string contain "-" (single hyphen) **tempMessage="hello--Name:xyz";(its splitting this) and if the tempMessage = "e-mail" it is splitting this too **

<c: forEach var="message" items="${fn: split(tempMessage, '--')}" varStatus="tempLoop">

the code should split only when string contain "--"(double hyphen) its should not split when string has"-"(single hyphen)

1 Answer 1

1

According to the JSTL spec, the ${fn:split()} uses under the covers StringTokenizer and the second argument is interpreted as "delimiters". So in the end the string will be split on each individual character found in the "delimiters" argument and the observable result is indeed as expected, albeit unintuitively because it contradicts the behavior of similarly named String#split() method.

Since EL version 2.2 (released at 2009 already) you can just invoke String#split() directly using the EL method syntax. So your functional requirement can be achieved as follows:

<c:forEach var="message" items="${tempMessage.split('--')}" varStatus="tempLoop">

The ${fn:split()} is only useful when you want to split on (multiple) individual characters.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.