记leetcode 2022/5/29 每日一题String类的split方法的使用

leetcode每日一题

题目:leetcode468-验证IP地址

引发的思考

问题:

当我验证该字符串是否是一个符合IPV4地址的字符串,使用s.split(“.”)的时候,出现了问题。得到的子字符串数组是长度为0的数组,惊讶!!!立刻去翻底层实现。

String类split方法底层实现源码分析

①先说明,平时我们使用得最多的是String.split(String regex)是采用第二参数默认是0的情况去进行的,底层还是调用split(String regex , 0);

public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        //如果regex只有一个字符且不是需要转义的字符,或者第一个字符为'\\',第二个字符既不是是数字也不是字母,则直接在String类的split方法解决;如果不满足上述条件,则regex字符串为一个正则表达式,需要去Pattern类解决。
        
		//ch 保存regex分割符
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
        	// off代表每个字符串起始位置
            int off = 0;
            // next代表的是ch字符的位置
            int next = 0;
            // limited代表分割后的最大字符串的个数
            boolean limited = limit > 0;
            // 结果保存
            ArrayList<String> list = new ArrayList<>();
            //循环获取regex分割符的位置,如果为-1,则已经分割完了
            while ((next = indexOf(ch, off)) != -1) {
            //当有限制最终分割后的字符串个数且结果个数小于限制数-1
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
            //当已经分割得到limit-1个字符串后,将剩下的所有字符都归为一个字符串,放入结果数组中
            // 注意当limited为false时,即limit <= 0 时,是尽可能地去分割字符串,无限制结果个数。
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            //没有进去while循环,即找不到分割符
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            // 貌似没用???
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            // 当limit为0时,将分割完最后是空的字符串全部删除; 也意味着当limit为-1时,不回删除最后的空字符串。
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        //把regex当成正则表达式
        return Pattern.compile(regex).split(this, limit);
    }

②split方法的regex代表的是分割符, 可以是字符,也可以是正则表达式。

总结

问题解决:由源码可知,当调用s.split(“.”),它会去走Pattern类的正则表达式匹配。但是“.”并不是一个正确的正则表达式,所以最后会得到一个空结果。正确的使用应该是使用转义字符:s.split(“\.”)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值