7
\$\begingroup\$

I want to split the following String into java ArrayList

"[\"1-72\",\"1-111\",\"1-4\"]"

I had wrote a very rough solution but not sure how to enhance it:

String sample = "\"[\"1-72\",\"1-111\",\"157f9d3dfb4-4\"]\"";
sample =  sample.replaceAll("\"", "");      //to replace double quote
sample = sample.replaceAll("\\[", "").replaceAll("\\]","");    //to replace open and close square bracket
List<String> sampleList= Arrays.asList(sample.split(","));
sampleList.forEach(System.err::println);
\$\endgroup\$
10
\$\begingroup\$

There are many encoding formats that could lead to this string. One of them is JSON. So instead of writing the splitting code yourself, you should use one of the many existing JSON parsers. Have a look at the awesome-java project at GitHub to find one.

Then, your code should become as simple as:

JSONArray sampleList = JSON.parseArray(sample);
\$\endgroup\$
6
\$\begingroup\$

I would say it depends on what the code is for.

If it's a write once quick solution for some small task your current code is fine as it is. The idea here is that if it no longer does what you want, you don't mind rewriting it entirely since it was a quick and (not so) dirty solution in the first place.

If instead you intend to use this as part of a bigger project that you plan on maintaining/improving you might want to look into JSON-parsing like Roland Illig suggests.


You didn't mention where the string originates from. If you're able to change that code as well you can also take a look at alternatives. For example JSON/XML serialisation/deserialisation of an entire class.

\$\endgroup\$
4
\$\begingroup\$

If you're sticking with vanilla Java (which I think is fine), then you're right that cleaning your String, splitting it, then converting to an ArrayList are 3 separate actions.

However, one thing that can clean up your solution a little is to realize replaceAll()'s first parameter is a regular expression instead of an exact String that replaceAll() checks for. So you can squish your method into:

String sample = "\"[\"1-72\",\"1-111\",\"157f9d3dfb4-4\"]\"";
sample =  sample.replaceAll("\"|\\[|\\]", "");   //to replace double quote and braces
List<String> sampleList= Arrays.asList(sample.split(","));
sampleList.forEach(System.err::println);

It's debatable whether or not that's actually cleaner since we're checking for 3 characters in one method, but regex is pretty powerful, so keeping this in mind might help you find a more objectively cleaner option in the future if you need to do a bunch of manipulations to a Strings.

I'm not sure how familiar you are with regular expressions, you can read more here, but the idea with this one is that a | pretty much means or, so this will replace a character if it's a " or a [ or a ].

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.