首页 > 代码库 > 如何把一段逗号分割的字符串转换成一个数组?

如何把一段逗号分割的字符串转换成一个数组?

public class StrSplit2{    public static void main(String[] args) {        String orgStr="a,b,c,d,e,f";          String [] result = orgStr.split(",");                for(int a = 0;a<result.length;a++){           System.out.print(result[a]+"\t");        }    }}

 

import java.util.StringTokenizer;//方案二:public class StrSplit{    public static void main(String[] args) {                String orgStr="ab,b,c,d,e,f";          StringTokenizer  tokener = new StringTokenizer(orgStr,",");        String [] result = new String[tokener .countTokens()];                  int i=0;        while(tokener. hasMoreTokens ())             {               result[i++]=tokener.nextToken();             }        for(int a = 0;a<result.length;a++){           System.out.print(result[a]+"\t");        }    }}

 

如何把一段逗号分割的字符串转换成一个数组?