Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string of values into array?

I have a string, ie 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17. How do I get each value and convert it into an array? [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]. I can't find any suggestions about this method. Can help? I did try using regex, but it just simply remove ',' and make the string into one long sentence with indistinguishable value. Is it ideal to get value before and after ',' with regex and put it into []?

like image 881
anggor Avatar asked May 11 '26 07:05

anggor


2 Answers

You could use following solution

String dummy = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] dummyArr = dummy.split(",");
like image 90
Gökhan Polat Avatar answered May 12 '26 21:05

Gökhan Polat


Try this to convert string to an array of Integer.

String baseString = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] baseArray = baseString.split(",");

int[] myArray = new int[baseArray.length];
for(int i = 0; i < baseArray.length; i++) {
    myArray[i] = Integer.parseInt(baseArray[i]);
}
like image 41
Ravi Khunt Avatar answered May 12 '26 20:05

Ravi Khunt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!