如何提取的android这个字符串变量?字符串、变量、android

2023-09-04 03:44:36 作者:╭ァ℡卟想洅僾铥妳づ

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]

我想这个文件中像 [1,低层次的编程语言,真] 等分成5种类型的字符串变量。

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

推荐答案

您可以拆分一个简单的正则表达式:

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