在Android中将字符串转换为字符串数组

我有 3 个这样的字符串:

  1. ["state","in",["assigned", "partially_available"]]

  2. ["state","in",["confirmed", "waiting"]]

  3. ["state","in",["confirmed", "waiting", "assigned"]]

现在我想将每个字符串转换为 2 个数组,其中前 2 个元素是一个数组,其他元素是一个数组(例如:)["state", "in"] ["asigned", "partially_available"]。有没有办法做到这一点?非常感谢!


撒科打诨
浏览 1004回答 2
2回答

侃侃尔雅

尝试这个。公共类 TestArray{public static void main(String[] args) {    String string = "[state,in,[assigned, partially_available]]";    String[] tempstr = string.split("\\["); //remove "["    String arrayFirstElement = tempstr[1].substring(0,tempstr[1].length()-1);    String arraySecondElement = tempstr[2].substring(0, tempstr[2].length()-2);    System.out.println("arrayFirstElement : " + arrayFirstElement);    System.out.println("arraySecondElement : " + arraySecondElement);    String[] strArray = new String[2];    strArray[0] = arrayFirstElement;    strArray[1] = arraySecondElement;    System.out.println("strArray[0] : " + strArray[0] + " AND strArray[1] : " + strArray[1]);}}

料青山看我应如是

String s = "[state,in,[assigned, partially_available]]";s = s.substring(1,s.length()-1); // removes first and last square bracketString[] sArr = s.split(",(?! )"); // split only the comma which is not followed by a space characterString[] newArr1 = {sArr[0],sArr[1]}; // first array containing ["state","in"]String temp = sArr[2].substring(1,sArr[2].length()-1); // remove the square brackets from the inner array String[] newArr2 = {temp.split(",")[0],temp.split(",")[1]}; // second array containing ["asigned", "partially_available"]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java