[英]How to extract this string variable in android?
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","Low-level programming language",true]
和其他文件分成5种类型的字符串变量。
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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.