/**
* Splits a string into multiple strings
*
* @param separator Separator char
* @param source_string Source string
* @return Array of strings
*
* source :
* http://www.particle.kth.se/~lindsey/JavaCourse/Book/Code/P3/Chapter24/SNAP/Worker.java
*/
public static String[] split(char separator, String source_string) {
// First get rid of whitespace at start and end of the string
String string = source_string.trim();
// If string contains no tokens, return a zero length array.
if (string.length() == 0) {
return (new String[0]);
}
// Use a Vector to collect the unknown number of tokens.
Vector token_vector = new Vector();
String token;
int index_a = 0;
int index_b;
// Then scan through the string for the tokens.
while (true) {
index_b = string.indexOf(separator, index_a);
if (index_b == -1) {
token = string.substring(index_a);
token_vector.addElement(token);
break;
}
token = string.substring(index_a, index_b);
token_vector.addElement(token);
index_a = index_b + 1;
}
return toStringArray(token_vector);
} // split
/**
* Convert a vector to an array of string
*
* @param vector Vector of string
* @return Array of string
*/
public static String[] toStringArray(Vector vector) {
String[] strArray = new String[vector.size()];
for (int i = 0; i < strArray.length; i++) {
strArray[i] = (String) (vector.elementAt(i));
}
return strArray;
}
您能分享錯誤? – gonzo
'每當我把拆分功能它顯示我一個錯誤'哪個錯誤?請在問題 – BackSlash
中粘貼堆棧跟蹤我假設它不是簡單地在行末尾缺少';'......? –