我有一个看起来像这样的字符串:
"["xxx","yyy","zzz"]"
谁能给我一个关于如何将其转换为如下所示数组的提示:
somestring[0]="xxx"
somestring[1]="yyy"
somestring[2]="zzz"
表达方式
"[\"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
肮脏的黑客
String s1="[\"xxx\",\"yyy\",\"zzz\"]";
String s2= s1.replaceAll("\"", "" );
s2=s2.replace("[", "");
s2=s2.replace("]", "");
String ar[] =s2.split(",");
查看输入字符串模式,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]