-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
  • 808
  • 1
  • 9
  • 22
Kiran
  • 20,167
  • 11
  • 67
  • 99

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
  • 34,993
  • 17
  • 75
  • 115
epoch
  • 16,396
  • 4
  • 43
  • 71
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
  • 34,993
  • 17
  • 75
  • 115
  • 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,479
  • 3
  • 16
  • 28
0

You can use split method on String.

String str = "1,2,3,4";
String[] listStr = str.split(",");
TheEwook
  • 11,037
  • 6
  • 36
  • 55