How to split java string to get 5 substring on the delimiter “,”

I am having a java string as response from server["This is my "json response", "jshj" "," "sdjfh,sfjg" ",""Abs, "jshj" ",""Abs, "jshj" ",""Abs, "jshj"",""Abs, "jshj" "].
I want to split the string to 5 substring on the delimiter “,” so that I can store the result into array.

Expected output is :

This is my “json response”, “jshj” “sdjfh,sfjg” “Abs, “jshj” “Abs,
“jshj” “Abs, “jshj” “Abs, “jshj”

Following is my approach:

String arrayGenerated[] =result.replaceAll("[\[\]"]", "").split(__________,5);
        return arrayGenerated;

Please suggest what should I mention in the split part.

4 Answers

After parsing your server response you can use String.split(“regular expression”) method to split the response.

String a = "["This is my "json response", "jshj" "," "sdjfh,sfjg" ",""Abs, "jshj" ",""Abs, "jshj" ",""Abs, "jshj"",""Abs, "jshj" "]";
String arr[] = a.split("","");

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class Test1222 {
    public static void main(String[]args)
        String fileName = "C:\filepath";
        List<String> list = null;
        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
            list = stream.collect(Collectors.toList()); 
        } catch (IOException e) {
        String str = list.get(0);
        String str1 = str.replaceAll("\[", "").replaceAll("\]","");

        String[] strArr = str1.split(",");
        List<String> resultArr = Arrays.asList(strArr);
        List<String> result =;




You can do it as follows:

public class Main {
    public static void main(String[] args) {
        String resString = "["This is my "json response", "jshj" "," "sdjfh,sfjg" ",""Abs, "jshj" ",""Abs, "jshj" ",""Abs, "jshj"",""Abs, "jshj" "]";

        String str = resString.replaceAll("\[", "").replaceAll("\]", ""); // Remove [ and ]

        String substrs[] = str.split("",""); // Split at ","
        for(int i=0;i<substrs.length;i++)
            substrs[i]=substrs[i].trim(); // Trim each substring
        int li=substrs.length-1; //Index of last substring in the array
        substrs[li]=substrs[li].substring(0,substrs[li].lastIndexOf(""")).trim(); // In the last String, remove extra " from the end

        for(String substr:substrs)


"This is my "json response", "jshj"
"Abs, "jshj"
"Abs, "jshj"
"Abs, "jshj"
"Abs, "jshj"

You can pass the server response and then split the response as follows,

String str = "["This is my "json response", "jshj" "," "sdjfh,sfjg" ",""Abs, "jshj" ",""Abs, "jshj" ",""Abs, "jshj"",""Abs, "jshj" "]";

str = str.replaceAll("\[", "").replaceAll("\]", ""); // To remove []

List<String> list = Arrays.asList(str.split(",")); // Simply separate words by comma to a list that you can iterate

for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
    String next =;

And if you need to remove double quotes from the server response you can use the following statement,

str.replaceAll(""", "")

Archive from:

Leave a Reply

Your email address will not be published. Required fields are marked *