user4129529
user4129529

Reputation:

Convert string containing an array to array object

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]

Upvotes: 0

Views: 2228

Answers (1)

David.Jones
David.Jones

Reputation: 1411

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(",");

Upvotes: 4

Related Questions