方法一:java
String a = "1,2,3,4,5,6" String str[] = a.split(","); int array[] = new int[str.length]; for(int i=0;i<str.length;i++){ array[i]=Integer.parseInt(str[i]); }
方法2:code
String str = "1,2,3,4,5,6" public int[] StringtoInt(String str) { int ret[] = new int[str.length()]; StringTokenizer toKenizer = new StringTokenizer(str, ","); int i = 0; while (toKenizer.hasMoreElements()) { ret[i++] = Integer.valueOf(toKenizer.nextToken()); } return ret; }
使用String.split方法時要注意的問題:it
"aaa*bbb*ccc","aaa+bbb+ccc","aaa|bbb|ccc" 要用"//*","//+","//|"轉義後便可獲得正確的結果。class
String[] aa = "aaa*bbb*ccc".split("//*"); String[] aa = "aaa+bbb+ccc".split("//+"); String[] aa = "aaa|bbb|ccc".split("//|");
當用"."分隔的時候, 要用"[.]" 進行分隔。方法