Two Laptop Bag
The moose likes JSP and the fly likes jstl function to split string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jstl function to split string" Watch "jstl function to split string" New topic

jstl function to split string

Anand Satchin

Joined: Mar 24, 2005
Posts: 10
I wanted to split a given string using jstl 1.2 eg:

Bean thesis.url contains " :"
which needs to be splited into

<c:set var="url">
<c:out value="${thesis.url}" />

<c:set var="offUrls" value="${fn:split(url,' : ')}" />
<c:forEach items="${offUrls}" var="link">
<a href=" <c:out value='${link}' />" target="_blank">
<c:out value="${link}" />


But the output is not want I wanted which is
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Anand Satchin wrote:
I tried another way, and its dint work either.
<c:set var="_split" value= " : "/>
<c:set var="offUrls" value="${fn:split(url,_split)}" />

"dint" is not a word -- please use real words when posting to the forums.

Also "dint work" doesn't help us help you. Please read ItDoesntWorkIsUseless and post more info so we can help you get this working. There's no reason it should not.

[Asking smart questions] [About Bear] [Books by Bear]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I think if you don't concentrate quite as much on what you want to happen, it should become quite obvious why you get the result you do.

You split on the ":" character. There are three such characters in the string and therefore you get four strings as the output. So clearly it was a really bad choice to use that character as the separator between the two strings you actually want as the output. I would suggest going back and fixing that bad choice. Use a separator character which isn't going to appear in the strings you want to be separated.
Anand Satchin

Joined: Mar 24, 2005
Posts: 10
The bean's attribute value is retrieved from database which in the form of " :"
i.e urls seperated by "space colan space"
Since it is a legacy system, I cannot do anything about it.

I could have used scriptlets to split them but would like to do it in a cleaner way.

<c:set var="url">
<c:out value="${thesis.url}" />

String str = (String)pageContext.getAttribute(url);
String [] strArray = str.split(" : ");

<c:forEach items="<%=strArray%>" var="link">
<a href=" <c:out value='${link}' />" target="_blank">
<c:out value="${link}" />
Anand Satchin

Joined: Mar 24, 2005
Posts: 10

fn:split will split the string on any of the delimiter characters, so in my case both space and :.
The solution is to do a fn:replace first:

<c:set var="urls" value=" :"/>
<c:set var="urls" value="${fn:replace(thesis.url, ' : ', '|')}"/>
Now use fn:split(urls, '|')

but it would be easier to use <c:forTokens/>

<c:forTokens items="${urls}" delims="|" var="url">
I agree. Here's the link:
subject: jstl function to split string
It's not a secret anymore!