Using Java 8's stream library, we can make this a one-liner (albeit a long line):
String str = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
int[] arr = Arrays.stream(str.substring(1, str.length()-1).split(","))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(arr));
substring
removes the brackets, split
separates the array elements, trim
removes any whitespace around the number, parseInt
parses each number, and we dump the result in an array. I've included trim
to make this the inverse of Arrays.toString(int[])
, but this will also parse strings without whitespace, as in the question. If you only needed to parse strings from Arrays.toString
, you could omit trim
and use split(", ")
(note the space).