How to extract this string variable in android?

后端 未结 1 341
独厮守ぢ
独厮守ぢ 2021-01-28 04:25
String test=
      [\"1\",\"Low-level programming language\",true],
      [\"2\",\"High level programming language\",false],
      [\"3\",\"Machine language\",false],[\"         


        
相关标签:
1条回答
  • 2021-01-28 04:38

    You could split on a simple regex:

    String [] splitStrings = test.split("\\],\\[");
    

    You don't want to split on just comma because you only want the commas between the square brackets.

    Here is a more complete example (and a regex that holds onto the brackets if you want)

    public static void main(String []args){
        String test="[\"1\",\"Low-level programming language\",true],[\"2\",\"High level programming language\",false],[\"3\",\"Machine language\",false],[\"4\",\"All of the above\",false],[\"5\",\"None of these\",false]";
        String [] splitStrings = test.split("(?!\\]),(?=\\[)");
    
        System.out.println(splitStrings[0]);
        System.out.println(splitStrings[1]);
        System.out.println(splitStrings[2]);
        System.out.println(splitStrings[3]);
        System.out.println(splitStrings[4]);
    }
    
    0 讨论(0)
提交回复
热议问题