方法一、
- 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 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]);
使用String.split方法时要注意的问题:
"aaa*bbb*ccc","aaa+bbb+ccc","aaa|bbb|ccc" 要用"//*","//+","//|"转义后即可得到正确的结果。
String[] aa = "aaa*bbb*ccc".split("//*");
String[] aa = "aaa+bbb+ccc".split("//+");
String[] aa = "aaa|bbb|ccc".split("//|");
当用"."分隔的时候, 要用"[.]" 进行分隔。