替代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);
}
/**
* 用分隔符将源串分为数组(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);
}
相关文章推荐
- String.split方法对"."分割的处理
- 对String s = "11.1,56.1,2.9,34.3,1.03,24.2" 不能使用jdk自带排序方法
- String.split方法分隔特殊字符"/"
- String.split("","")与StringUtil.split("","")的区别
- String.split("","")与StringUtil.split("","")的区别
- java的String的equals方法和"=="比较
- string.Format出现异常"输入的字符串格式有误"的解决方法
- SplitConcatWithAMP----Array转换为String,"&"连接;String转换为Array,"&"切割
- js中的字符串(string)的一些方法:toUpperCase(),indexOf(""),length
- java中split()方法的特殊分割符"|"和"."
- request.getRealPath("") 的替代方法
- 方法getParaValString(tSet, "IPMMODULE"));
- String.split("") 你不知道的那些事情
- split("|")正确使用方法split("\\|")
- ognl.OgnlException: target is null for setProperty(null, "offset", [Ljava.lang.String;@1667f3c) 解决方法
- "ERROR:Malformed string"错误的解决方法
- Request.QueryString["**"]发生“未将对象引用设置到对象的实例。”的解决方法
- 在gridview里对日期字段设置dataformatstring="{0:yyyy-MM-dd}"没有任何作用的解决方法
- 对于Name属性没有get/set方法引起的:target is null for setProperty(null, "x", [Ljava.lang.String;@1a48515)异常
- 按日期取得文件名的两种方法 fileName=DateTime.Now.ToString("yyyyMMddhhmmss");