- 论坛徽章:
- 0
|
如何将一个字符串按“,”号分隔,存入数组中?
刚才贴的比较乱,重发一个。
public String[] Split(String strSource, String operator) {
Vector vTest = new Vector();
int startIndex = 0;
int i=0;
startIndex = strSource.indexOf(operator);
if (startIndex == -1 ) {
vTest.add(strSource);
}else {
String thisStr = strSource;
while (startIndex != -1) {
vTest.add(thisStr.substring(0,startIndex));
thisStr = thisStr.substring(startIndex+1);
startIndex = thisStr.indexOf(operator);
i++;
}
vTest.add(thisStr.substring(startIndex + operator.length()));
}
String[] sRet = new String[vTest.size()];
vTest.copyInto(sRet);
return sRet;
} |
|