-3

I want to separate a string and put it into an array.

Example:

id = 12,32,544,877,136,987 

arraylist: [0]-->12
           [1]-->32
           [2]--544
           [3]-->877
           [4]-->136
           [5]-->987

How to do that?

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
prog.cpp
  • 25
  • 1
  • 7
  • I just want to trim the string where the "," is and pass it to the arraylist so i can return the last index for my program :) – prog.cpp Dec 05 '17 at 09:48
  • 3
    This question is so basic and was asked 1000 times. Why you use an `ArrayList` at all if you are new to the language? Use a generic `List`. – Tim Schmelter Dec 05 '17 at 09:49
  • is there a particular reason for the choice of an [ArrayList](https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx) ??? Might it be that this is homework? and the `ArrayList` a prerequesite of the excercise? – Mong Zhu Dec 05 '17 at 09:50
  • `ArrayList`'s origin was back then when a `List` didnt exist yet. I suggest you to go with a `List`, since it provides advantages that an `ArrayList` can offer. – L. Guthardt Dec 05 '17 at 10:10

3 Answers3

1

If your id var is a String, you can use the Split method:

id.Split(',')
DavidWainwright
  • 2,895
  • 1
  • 27
  • 30
1

Try:

string[] arraylist = id.Split(',');
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
1

Can do something like this in java :

   ArrayList<String> idList = new ArrayList <String>();

   String id = "12,32,544,877,136,987";

   String idArr[] = id.split(",");
   for(String idVal: idArr){
       idList.add(idVal);
   }