我有一个看起来像这样的字符串:
"["xxx","yyy","zzz"]"
任何人都可以给我一个如何将其转换为如下所示的数组的提示:
somestring[0]="xxx"
somestring[1]="yyy"
somestring[2]="zzz"
答案 0 :(得分:2)
表达式
"[\"xxx\",\"yyy\",\"zzz\"]".replaceAll( "[\\\"\\[\\]]", "" ).split( "," );
将产生一个数组:
a[0] = "xxx";
a[1] = "yyy";
a[2] = "zzz";
我希望......
[1小时后......]
我确认:
public static void main(String[] args) {
for( String part : "[\"xxx\",\"yyy\",\"zzz\"]".replaceAll( "[\\\"\\[\\]]", "" ).split( "," )) {
System.out.println( part );
}
}
输出:
xxx
yyy
zzz
答案 1 :(得分:1)
肮脏的黑客
String s1="[\"xxx\",\"yyy\",\"zzz\"]";
String s2= s1.replaceAll("\"", "" );
s2=s2.replace("[", "");
s2=s2.replace("]", "");
String ar[] =s2.split(",");
答案 2 :(得分:0)
查看输入字符串模式,substring
也可以工作:
String inStr = "\"[\"xxx\",\"yyy\",\"zzz\"]\"";
String [] inArray = inStr.substring(0,inStr.length()-2).substring(2).split(",");
System.out.println(Arrays.toString(inArray));
将打印:
["xxx", "yyy", "zzz"]
如果您想删除引号("),,请写如下:
String inStr = "\"[\"xxx\",\"yyy\",\"zzz\"]\"";
String [] inArray1 = inStr.substring(0,inStr1.length()-2)
.substring(2).replaceAll("\"","").split(",");
System.out.println(Arrays.toString(inArray1));
或者你可以这样做:
String inStr = "\"[\"xxx\",\"yyy\",\"zzz\"]\"";
String [] inArray = inStr.replaceAll( "[\\\"\\[\\]\"]", "").split( "," );
System.out.println(Arrays.toString(inArray0));
以上两个将打印:
[xxx, yyy, zzz]