Umesh Kacha
Umesh Kacha

Reputation: 13686

Java String.split multiple character delimiter

I would like to parse just one long line from file based on " and , and [ and ] and { and }

For e.g

{"ColumnTypes":"int32","fstring32","int32"],"ColumnNames":"ProductId",","ProductName","Quantity"]}

I actually need two different array from this line column types and column names. I tried string.split("\\W") but it is not working.

Please guide. Thanks in advance.

Upvotes: 1

Views: 2426

Answers (2)

Bohemian
Bohemian

Reputation: 425398

You need to split on multiple \\W chars (add a +), but first trim off such chars from the front, so you don't get a blank first element:

String[] array = string.replaceAll("^\\W+", "").split("\\W+");

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

The simplest way (with regex) would be to allow more than one non-word character to match:

string.split("\\W+")

Upvotes: 1

Related Questions