您的位置:首页 > 其它

替代String.split("")的方法

2006-04-20 13:43 417 查看
String.split返回将一个字符串由分隔符分隔而成的数组,但在这个方法里当split="|"时似乎把它拆分成一个个的char数组;另外,StringTokenizer也有类似的功能,但它里面的分隔符是单个的char而非整个串,而且它没有返回值,只能向Enumeration一样一个个的去遍历。

/**
* 用分隔符将源串分为数组(String的该方法对split="|"失效,当split="*"时肯定出错!)
* @param source 源字符串
* @param split 分隔符
* @return 数组
* @throws Exception
*/
public static String[] splitStr(String source,String split) throws Exception{
if (source==null || source.trim().equals("")
|| split==null || split.trim().equals("")){
return new String[]{};
}
int len = split.length();
int index = -1;
ArrayList list = new ArrayList();
while ((index=source.indexOf(split)) != -1){
list.add(source.substring(0,index));
source = source.substring(index+len);
}

if (!source.equals("")) list.add(source);
return DataTypeConverter.listToarrayString(list);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐