A Funny Java Flavoured Look at the World

Thursday, November 30, 2006

trouble using | (pipe) with the String.split method

I came across this little problem whilst coding this week and it was driving me crazy and it is only a little problem so I wasn't going to blog about it but I couldn't find any information on it anywhere so I thought I would write a blog entry in case someone else has this problem.

The problem came when I tried to using String.split method, you replace String with an actual String and not the static object. Anyway I had written a log file and used | (pipe) as a delimeter so that when I got the log file in my code I could just do a split("|") and then it would chop each line into a nice array of Strings which I could loop through and search and take out the values I wanted.

The problem I was getting was when I did split

myString.split("|")

it split the string on every single character, which was quite a shock. I tried to goggle the problem by typing in things like

"using pipes with split()"
"pipe metacharacters"

I knew from my study of the SCJP 5 that there is some tricky characters in regex (regular expressions) called metacharacters but I couldn't find | included in any of them and wasn't sure why it was acting funny. I have blogged about metacharacters before in a blog entry called
\n - newline saves the day and formats all my troubles away

In the end I found that to successfully use the pipe in a regular expression I had to add a double backslash like so

myString.split("\\|");

and then it worked fine.

I thought I would blog about this just in case someone else runs across this problem. I'm not sure perhaps its something which everyone knows not to do (except me obviously) , I know pipes are used for adding things together but I still don't really understand why it would split on everyone single space

96 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home