String源码 spilt

版权声明: https://blog.csdn.net/xmfsamsara/article/details/80354906

public String[] split(String regex, int limit) {

        return Pattern.compile(regex).split(this, limit);

    }

    public String[] split(String regex) {

        return split(regex, 0);

    }

    //javase 1.7api中String类的split方法

    public String[] split(String regex, int limit) {

        char ch = 0;

		
		  if (((regex.value.length == 1 &&             //长度等于1
		  
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||         //正则的第一个字符不是在  ".$|()[{^?*+\\" 中
			 
             (regex.length() == 2 &&                               //长度等于2的同时
			 
              regex.charAt(0) == '\\' &&                           //第一个字符为 \            
			  
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&       // 这种判断是 ch < '0' && ch > '9' 时, 返回 true ,  也就是: (负数 | 负数) <0 ; 下同理
			  
              ((ch-'a')|('z'-ch)) < 0 &&
			  
              ((ch-'A')|('Z'-ch)) < 0)) &&
			  
            (ch < Character.MIN_HIGH_SURROGATE ||
			
             ch > Character.MAX_LOW_SURROGATE))
			 


        {

            int off = 0;

            int next = 0;

            boolean limited = limit > 0;

            ArrayList<String> list = new ArrayList$amp; 

            while ((next = indexOf(ch, off)) != -1) {

               if (!limited || list.size() < limit - 1) {

                    list.add(substring(off, next));

                    off = next + 1;

                } else {             // last one ,如果传入返回数组长度,则 打到限制长度后,添加off 到原字符串末尾的字符串。

                    //assert (list.size() == limit - 1);

                    list.add(substring(off, count));

                    off = count;

                    break;

                }

            }

            // If no match was found, return this

            if (off == 0)                                      

                return new String[] { this };

            // Add remaining segment

            if (!limited || list.size() < limit)   // 问题2的解答:如果未到长度限制,或者未传入第二个参数,添加 off 到末尾的字符串

                list.add(substring(off, count));

            // Construct result

            int resultSize = list.size();

            if (limit == 0)

                while (resultSize > 0 && list.get(resultSize-1).length() == 0)  

                    resultSize--;

                                                                // 问题1解答:如果我们不传入split的第二个参数,或者传0,那么最后此方法会从添加的最后一个元素开始,循环删除掉长度为0的“”字符串直到遇到第一个不为空的字符串(从后面开始的,所以就是遇到最后一个不为空的字符串时,停止删除),;

            String[] result = new String[resultSize];

            return list.subList(0, resultSize).toArray(result);   //subList此方法返回父list的一个视图

        }

        return Pattern.compile(regex).split(this, limit); 

            /// 关于这个的话,需要了解一下正则表达式了,我给一篇好问题:https://my.oschina.net/u/1187481/blog/215379


}

先上源码,之前做项目这里遇到了一个坑,没有传limit,默认为0,结果分割结果数组末尾为空的字符被删掉了,导致转换map短了。  分析源码发现 limit传-1或者其他负数可以返回完全的分割结果,不传或者传0会删除末尾为空的分割,知道遇到一个不为空的分割,传递limit会返回前limit个分割结果


猜你喜欢

转载自blog.csdn.net/xmfsamsara/article/details/80354906