java 字符串split有很多坑,使用时请小心!!

System.out.println(":ab:cd:ef::".split(":").length);//末尾分隔符全部忽略 
System.out.println(":ab:cd:ef::".split(":",-1).length);//不忽略任何一个分隔符
System.out.println(StringUtils.split(":ab:cd:ef::",":").length);//最前面的和末尾的分隔符截取过滤掉了
System.out.println(StringUtils.splitPreserveAllTokens(":ab:cd:ef::",":").length);//不忽略任何一个分隔符 apache commons 输出: 4 6 3 6 

 

看到有人评论说StringUtils.split(":ab:cd:ef::",":")最前面的和末尾的分隔符全部都忽略  说的也不知道正不正确,我直接去看了一下源码。

我的jar包  commons-lang-2.5.jar包中查看相关操作源码


	private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens)
	{
	  if (str == null) {
	    return null;
	  }
	  int len = str.length();
	  if (len == 0) {
	    return ArrayUtils.EMPTY_STRING_ARRAY;
	  }
	  List list = new ArrayList();
	  int sizePlus1 = 1;
	  int i = 0;int start = 0;
	  boolean match = false;
	  boolean lastMatch = false;
	  if (separatorChars == null)
	  {
	    while (i < len)
	      if (Character.isWhitespace(str.charAt(i))) {
	        if ((match) || (preserveAllTokens)) {
	          lastMatch = true;
	          if (sizePlus1++ == max) {
	            i = len;
	            lastMatch = false;
	          }
	          list.add(str.substring(start, i));
	          match = false;
	        }
	        i++;start = i;
	      }
	      else {
	        lastMatch = false;
	        match = true;
	        i++;
	      } }
	  if (separatorChars.length() == 1)  //要分割的字符串为1位时。
	  {
	    char sep = separatorChars.charAt(0);  //获取要分割的字符
	    while (i < len) {                        //循环读取字符串的长度
    	/**
    	 * 获取字符串的第几位的字符  与 要分割的字符比较  ,其实最主要的方法也是这里,这里也实现了无字符的去除。
    	 * 如果字符串分割后每个字符与 要分割的字符比较,不等于的时候,match一直是false,如果等于则是true,然后进行截取。
    	 * 
    	 * 感觉用截取过滤这个词比较合适
    	 * match的值会依次是  
    	 * =========false
    	 * =========false
    	 * =========true   
    	 * =========true
    	 * =========false
    	 * =========true
    	 * =========true
    	 * =========false
    	 * =========true
    	 * =========true
    	 * =========false
    	 */
	   System.out.println("========="+match);
	      if (str.charAt(i) == sep) {           
	        if ((match) || (preserveAllTokens)) {   // 最主要的地方是这里
	          lastMatch = true;
	          if (sizePlus1++ == max) {
	            i = len;
	            lastMatch = false;
	          }
	          list.add(str.substring(start, i));
	          match = false;
	        }
	        i++;start = i;
	      }
	      else {
	        lastMatch = false;
	        match = true;
	        i++;
	      }
	    }
	  } else {
	    while (i < len)
	      if (separatorChars.indexOf(str.charAt(i)) >= 0) {
	    	 
	        if ((match) || (preserveAllTokens)) {
	          lastMatch = true;
	          if (sizePlus1++ == max) {
	            i = len;
	            lastMatch = false;
	          }
	          list.add(str.substring(start, i));
	          match = false;
	        }
	        i++;start = i;
	      }
	      else {
	        lastMatch = false;
	        match = true;
	        i++;
	      }
	  }
	  if ((match) || ((preserveAllTokens) && (lastMatch))) {
	    list.add(str.substring(start, i));
	  }
	  return (String[])list.toArray(new String[list.size()]);
	}
	

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值