Using the Java 8 thread library, we can make this one-line (albeit long):
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 elements of the array, trim removes any spaces around the number, parseInt parses each number, and we unload the result into an array. I have included trim to make it inverse to Arrays.toString(int[]) , but it will also parse strings without spaces, as in the question. If you only need to parse the strings from Arrays.toString , you can omit trim and use split(", ") (note the space).
Jeffrey Bosboom Sep 15 '14 at 0:43 2014-09-15 00:43
source share