0

I need to convert a string stored like,

textView.setText(myVar);

Here myVar = "23,45,64,78";

I would like it to convert into array like below,

int[] xAxis = new int[]{23,45,64,78};

How can I achieve this? Thanks

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33

1 Answers1

1

Try this:

String arr = "[23,45,64,78]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");

int[] results = new int[items.length];

for (int i = 0; i < items.length; i++) {
    try {
        results[i] = Integer.parseInt(items[i]);
    } catch (NumberFormatException nfe) {};
}
coder
  • 13,002
  • 31
  • 112
  • 214
  • Too fast for me but edit that response to remove the bracket at least. – Eric Fortin Feb 22 '14 at 03:18
  • Sorry, new here. I am getting values from textview like this 12 34 56 and how can i add quotes and brackets like this String arr = "[23,45,64,78]"; Thanks – user3339618 Feb 22 '14 at 03:37