2 回答
TA贡献1801条经验 获得超16个赞
尝试这个。
公共类 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]);
}
}
TA贡献1772条经验 获得超8个赞
String s = "[state,in,[assigned, partially_available]]";
s = s.substring(1,s.length()-1); // removes first and last square bracket
String[] sArr = s.split(",(?! )"); // split only the comma which is not followed by a space character
String[] 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"]
添加回答
举报