为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

撒科打诨 2021-09-29 15:09:26
我有 3 个这样的字符串:["state","in",["assigned", "partially_available"]]["state","in",["confirmed", "waiting"]]["state","in",["confirmed", "waiting", "assigned"]]现在我想将每个字符串转换为 2 个数组,其中前 2 个元素是一个数组,其他元素是一个数组(例如:)["state", "in"] ["asigned", "partially_available"]。有没有办法做到这一点?非常感谢!
查看完整描述

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


}

}


查看完整回答
反对 回复 2021-09-29
?
料青山看我应如是

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"]



查看完整回答
反对 回复 2021-09-29
  • 2 回答
  • 0 关注
  • 1003 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信