wood burning stoves 2.0
The moose likes Java in General and the fly likes String split - Regular expression 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 - Regular expression" Watch "String split - Regular expression" New topic

String split - Regular expression

Abi Raj

Joined: Jan 28, 2005
Posts: 28
I want to split a string into array of strings. the delimiter is ,.

for example the string when splited
"\\ma\\", "g,a", "\"AL","\"wA\"", in, "or"

should return an array of the following strings.


Can I use a regular expression and if so how?

Thanks in Advance.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33117

You are correct - Java 1.4 and higher have a split method that takes a regular expression:
String[] result = string.split(regExp)

The delimiter is not just a comma in your example though. It looks like it is a comma not surrounded by quotes or a comma followed by a space. I can't tell which one from the example.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Your string has to be adjusted for compiling and the closest I get is with the following code

The problem is that the sign \ is an escape karakter in Java.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link: http://aspose.com/file-tools
subject: String split - Regular expression
It's not a secret anymore!