aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jstl function to split string" Watch "jstl function to split string" New topic
Author

jstl function to split string

Anand Satchin
Greenhorn

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

Bean thesis.url contains "http:website1.com : http:website2.com"
which needs to be splited into
http:website1.com
http:website2.com

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

<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}" />

</c:forEach>

But the output is not want I wanted which is
http
website1.com
http
website2.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

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
Greenhorn

Joined: Mar 24, 2005
Posts: 10
The bean's attribute value is retrieved from database which in the form of "http:website1.com : http:website2.com"
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}" />
</c:set>

<%
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
Greenhorn

Joined: Mar 24, 2005
Posts: 10
Solution:
------------

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="http://website1.com : http://website2.com"/>
<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">
${url}
</c:forTokens>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jstl function to split string
 
Similar Threads
questions about JSTL variables
Problem with JSTL c:out tag
forEach nested loop
using EL to get the keySet of a HashMap
JSTL 1.1 - Get value from method of an Obj and set it to a variable