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;
}
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;
}
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());
}
}
}
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]);
}
}
}
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.
int count = st.countTokens();
String[] s = new String[count];
for (int i = 0; i<count ;i++) {
s[i] = st.nextToken();
}当然写一个专门的方法出来时间效率更高,在此不作考虑,仅供参考。