How to extract this string variable in android?

孤者浪人 提交于 2019-12-31 05:36:13

问题


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]

I want to separate this file like [1","Low-level programming language",true] and others into 5 types of string variables.


回答1:


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]);
}


来源:https://stackoverflow.com/questions/17887708/how-to-extract-this-string-variable-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!