The Splitting by Pipes Nightmare

Pipes of hell

Pipes of hell

Not many of you know this, but during normal non alien human hours I actually have a very convential non-geeky job, Im speaking ofcourse of the historical human trade of selling world wide websites to massive corporations (censored) who seem to enjoy the sadistic act of constantly purchasing out of date web (sights) from murky underground web traders. Anyways, today and during a normal day of work I came across of problem of splitting a string by a certain delimiter. “Why this is very trivial Mr Kay” you might rightly say and to that I salute you, this is indeed one of the most basic Java excercises that junior traders in web trade school go through, but when the string is more than 50 words amalgamated by pipes (“|”) you suddenly realize the Gods of binary systems are conspiring against you.

So to convey my point in a more structured manner (you know as we normally do in the trade) take the following snippet of code from an apache velocity template


#set($string = "BB XL|BatikhaSafra|BeingWeird")
#foreach($ramble in $string.split("|"))
$ramble
#end

The output expected should be:


BBXL
BatikhaSafra
BeingWeird

Yet in reality what was happening was that the output was something along those lines:


B
B
X
L
|
B
A
...

Hmmmm, something just doesnt feel right, I wonder what it is. Well needless to say, narcassit me just had to find out the solution on their own.

Well .. after around no less than 3 hours of excavation it turns out that the “|” is a java metacharacter and it was actually splitting over empty strings, something like this:


#foreach($ramble in $string.split(""))
$ramble
#end

Sucks …

The solution was then to escape it (as you do…). So here is the final unadultrated code:


#set($string = "BB XL|BatikhaSafra|BeingWeird)
#foreach($ramble in $string.split("[|]"))
$ramble

#end

Talk about pipe streaming! (lame I know)

Advertisements

3 thoughts on “The Splitting by Pipes Nightmare

  1. Pingback: If Google went down, would you still be able to code? | batikhasafra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s