public static String[] split(String str, String delimiter) {
ArrayList array = new ArrayList();
int index = 0;
int begin = 0, end;
String[] result = new String[0]; if (isEmpty(str)) {
/* modified by xmsong, 2002-12-10 */
// return new String[0];
return result;
} while (true) {
index = str.indexOf(delimiter, begin); if (index == begin) {
if (index >= 0) {
array.add("");
}
begin += delimiter.length();
} else if (index > begin) {
end = index; array.add(str.substring(begin, end)); begin = index + delimiter.length();
} else {
if ((begin >= 0) && (begin < str.length())) {
array.add(str.substring(begin));
} break;
}
} if (str.endsWith(delimiter)) {
array.add("");
} //modified by lusd at 2004/04/10
for (int i = array.size() - 1; i >= 0; i--) {
if (array.get(i).toString().equals(""))
array.remove(i);
} if (array.size() > 0) {
result = new String[array.size()]; array.toArray(result);
} return result;
}

解决方案 »

  1.   

    使用StringTokenizer的StringTokenizer(String str, String delim) 构造方法,指定分割符“,“即可import java.util.StringTokenizer;
    public class Test { public static void main(String[] args) {
    StringTokenizer st = new StringTokenizer("aaa,bbb,ccc,dd,eeeee",",");
    while(st.hasMoreTokens()){
    System.out.println(st.nextToken());
    }
    }
    }
      

  2.   

    StringTokenzier, 从JDK1.0 至今,它都是分割字符串的最好选择,甚至在很多场合优于split
      

  3.   

    小弟的可能要简单一些,顺便也测试了。import java.util.*;
    public class Test
    {
    public String[] split(String str,char ch)
    {
      int from = 0;
      int result = -1;
      Vector vec = new Vector();
      do
      {
      result = str.indexOf(ch,from);
      if(result == -1) break;
      vec.addElement(str.substring(from,result));
      from = result+1;
      }while(true);
      vec.addElement(str.substring(from,str.length()));
      String[] res = new String[vec.size()];
      for(int i = 0 ; i < vec.size() ; i++)
      {
       res[i] = vec.elementAt(i).toString();
      }
      return res;
    }
    public static void main(String[] args)
    {
    String str = "aaa,bbb,ccc,ddd";
    Test test = new Test();
    String[] res = test.split(str,',');
    for(int i = 0 ; i< res.length ; i++)
    {
    System.out.println(res[i]);
    }
    }
    }
      

  4.   

    StringTokenzier与split都可以,一楼的代码也太长了吧
      

  5.   

    我还是喜欢StringTokenzier,功能够强
      

  6.   

    StringTokenizer
    public StringTokenizer(String str)Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
      

  7.   

    既然是1.3版本,就用StringTokenizer类,在java.util包中     StringTokenizer st = new StringTokenizer("this,is,a,test",",");
         int count = st.countTokens();
         String[] s = new String[count];
         for (int i = 0; i<count ;i++) {
             s[i] = st.nextToken();
         }当然写一个专门的方法出来时间效率更高,在此不作考虑,仅供参考。