将字符串转换为int数组

时间:2014-02-22 03:11:27

标签: android

我需要转换存储的字符串,

textView.setText(myVar);

下面     myVar =“23,45,64,78”;

我希望它转换成如下所示的数组,

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

我怎样才能做到这一点?感谢

1 个答案:

答案 0 :(得分:1)

试试这个:

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) {};
}