0

Hello I have a string containing an array! I want to be able to construct this into an array but I cannot find any methods for doing so! Can someone help me, this is what my string looks like

[111111,111111,111111,111111,111111,111111,111111]

1
  • You're going to need to parse the string: start by removing the square brackets and then look for the commas (which will be your delimiters). Commented Dec 1, 2014 at 14:47

1 Answer 1

4

Just take out the square brackets then use the string split method, giving ',' as a delimiter.

String str = "[111111,111111,111111,111111,111111,111111,111111]"
//remove the brackets
//as backslash mentioned, str.substring is a better approach than using str.replaceAll with regex
str = str.substring(1, str.length()-1);
//split the string into an array
String[] strArray = str.split(",");
Sign up to request clarification or add additional context in comments.

7 Comments

To remove brackets you can avoid regexes by using str = str.substring(1, str.length()-1)
Thats a good idea that will solve my problem! how would I loop on each iem to do this though?
@RyanMcCleave for(String s : strArray) { ... }
I dont understand this? how would this recognise each of the 6 number groupings in the string?
The str.split method splits the overall large string into substring, each delimited by the ",". This results in an Array that contains each number grouping.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.