Split String into Array of Arrays

Convert string into an array of arrays in javascript

You can use JSON.parse() and .replace() to make your string a parsable string like so:

const my_string = "['page1',5],['page2',3],['page3',8]",stringified = '['+my_string.replace(/'/g, '"')+']';
console.log(JSON.parse(stringified));

How to split an array in java or Kotlin?

A simple solution is joining the original array into a string, split by space and then split again into the characters (here in kotlin):

arr.joinToString().split(" ").map{ it.split() }

This is not optimized but still in O(n) (linear complexity). It benefits from readability which most often should be preferred whereas performance should be addressed if this is critical to your task at hand.

How to store a split string as an array in Java?

in.split(" ", 2) creates an array of String elements. Arrays don't generally print easily. Using Arrays.toString() is a good way to get a nice String representation of the array object. If you want to store the array object, use the result of in.split(" ", 2), and format it as a string only when you need to.



Related Topics



Leave a reply



Submit