It's not a secret anymore!
The moose likes Java in General and the fly likes String Split problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Split problem" Watch "String Split problem" New topic

String Split problem

Andy Holm
Ranch Hand

Joined: Jul 03, 2008
Posts: 86
I have fields that look like this: "M|F".

I want to split that String on the "|". However, it doesn't seem to work when I run the following code:

The result I get is every character (M, |, and F) is in its own array index. What am I doing wrong?
Henry Wong

Joined: Sep 28, 2004
Posts: 20176

The "|" has special meaning in a regex -- it is a OR operation for search patterns. If you actually want the literal "|", then you need to escape it as "\\|".


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: String Split problem
It's not a secret anymore!