1

got a string that looks like this:

"abc", "def", "ghi"

I need to split string to array like this:

abc
def
ghi

I tried:

String [] strArray = sb.toString().split(" ,\""); 

but it doesn't work.

2
  • "it doesn't work" and your example doesn't make much sense... could you rephrase yourself please? Commented Oct 5, 2014 at 6:15
  • it doesnt split it. strArray.length() is equal to 1 Commented Oct 5, 2014 at 6:34

2 Answers 2

1

You firstly need to remove first and last quotation from string:

sb = sb.substring(1, sb.length() - 1); //remove first and last character

after above line of code your sb will be:

abc", "def", "ghi

and then try this regex:

String[] strArray = sb.split("\"\\s*,\\s*\"");

its split on the basis of quote (any no of space) comma (any no of space) quote

Sign up to request clarification or add additional context in comments.

Comments

0

the split method accept regex

consider the following code

str.split("(\", \")+|(\")+")

you will get a four elements array

[, abc, def, ghi]

As suggested by Zaheer you can remove the first "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.