数字型String字符串转换成int型数组
数字型String字符串转换成int型数组
方法一、
Java代码Stringstr="1,2,3,4,5,6"publicint[]StringtoInt(Stringstr){intret[]=newint[str.length()];StringTokenizertoKenizer=newStringTokenizer(str,",");inti=0;while(toKenizer.hasMoreElements()){ret[i++]=Integer.valueOf(toKenizer.nextToken());}returnret;}
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;}
方法二、
Java代码Stringa="1,2,3,4,5,6"Stringstr[]=a.split(",");intarray[]=newint[str.length];for(inti=0;i<str.length;i++){array[i]=Integer.parseInt(str[i]);
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("//|");
当用"."分隔的时候, 要用"[.]" 进行分隔。
如果觉得《把String字符串转换为Int数组》对你有帮助,请点赞、收藏,并留下你的观点哦!