分享分割字符串
public String[] strchange(String str, char ch) {
int a = 0; //存放ch的数量
for (int i = 0; i < str.length(); ++i)
if (ch == str.charAt(i))
++a;
String[] arr = new String[a + 1];
Integer[] arrint = new Integer[a];
int b = 0; //增量,实现同步
for (int i = 0; i < str.length(); ++i)
if (ch == str.charAt(i)) {
arrint[b] = Integer.valueOf(i + 1); ++b;
}
int begin = 0; int ab = 0; //增量
for (int i = 0; i < arrint.length; ++i) {
arr[ab] = str.substring(begin, arrint[i].intValue() - 1);
begin = arrint[i].intValue(); ++ab;
}
arr[(arr.length - 1)] = str.substring(arrint[(arrint.length - 1)].intValue());
return arr;
}
大致思想就是,先计算出字符串中,需要分割字符串的位置,然后依据存放位置的数组来遍历分割字符串