Home » Java » How to spilt a String Using comma in java-Exceptionshub

How to spilt a String Using comma in java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a string:

"1234/1234/abcd/4567/acds.jpg,1234/1234/abcd/4567/18454_048.tif"

I need to split it on the comma;

"1234/1234/abcd/4567/acds.jpg,1234/1234/abcd/4567/18454_048.tif".split(",");

which works fine for the above input. However, some of my inputs are like this:

"1234/1234/abcd/4,567/acds.jpg,1234/1234/abcd/4567/18454_048.tif"

And it is here that my approach fails – it splits the input into three strings:

"1234/1234/abcd/4"
"567/acds.jpg"
"1234/1234/abcd/4567/18454_048.tif"

But this isn’t what I want. In this case I want the output to be two strings:

"1234/1234/abcd/4,567/acds.jpg"
"1234/1234/abcd/4567/18454_048.tif"

How can I change my approach so that I can still separate out each individual path without chopping one of them up if it also contains a comma?

How to&Answers: