0

I have the following String

[http://images.com/1.jpg, http://images.com/2.jpg, http://images.com/3.jpg]

I want to store the contents of this array inside a string array or array list of type string.

I tried using .split method, but it fails mainly because the string also contains the brackets at the beginning.

tony9099
  • 4,567
  • 9
  • 44
  • 73

5 Answers5

3
String[] splittedString = theString.substring(1, theString.length()-1).split(", ") 

Notice space after comma in the split method.

ghdalum
  • 891
  • 5
  • 17
2

Use substring to exclude the string from the brackets:

mystring = mystring.substring(1,mystring.length()-1);

And then the split:

String[] myarray = mystring.split(", ");
Loki
  • 4,065
  • 4
  • 29
  • 51
1
String arry[] = yourstr.replace("[", "").replace("]", "").split(",");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Escape the first and the last characters of your String containing the brackets before using the split() method, like this :

yourString= yourString.substring(1, yourString.length()-1));
// do your split() method
Guillaume M
  • 470
  • 1
  • 5
  • 12
-1

Optionally you can use StringTokenizer.

commit
  • 4,777
  • 15
  • 43
  • 70