This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jstl function to split string

 
Anand Satchin
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64186
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20187
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic