-8

Is there any built-in method in Java which allows us to convert a string into an list of strings. For eg:

"1,2,3,4" to "1","2","3","4"

Help would be appreciated

swapnil7
  • 728
  • 1
  • 7
  • 21
Kiran
  • 19,739
  • 10
  • 64
  • 98

4 Answers4

1

Yeah, using the split method

String a = "1,2,3,4";
String[] aa = a.split(",");

Into a list:

List<String> aaa = Arrays.asList(aa);
Ruchira Gayan Ranaweera
  • 33,712
  • 16
  • 72
  • 110
epoch
  • 16,149
  • 3
  • 42
  • 69
0

You can create String[] by split "1,2,3,4" by ,. Then convert that in to List by Arrays.asList()

    String str="1,2,3,4";
    String[] arr=str.split(",");
    List<String> list=Arrays.asList(arr);
Ruchira Gayan Ranaweera
  • 33,712
  • 16
  • 72
  • 110
  • Thanks Bro. But output is [1, 2, 3, 4]. I am looking for something like this: "1","2","3","4". – Kiran Feb 20 '14 at 15:00
0
String s = "1,2,3,4";
String[] tokens = s.split(",");
List<String> tokenlist=Arrays.asList(tokens);
Rahul
  • 3,441
  • 3
  • 15
  • 27
0

You can use split method on String.

String str = "1,2,3,4";
String[] listStr = str.split(",");
TheEwook
  • 10,817
  • 6
  • 33
  • 54