【常用函数】split()

小凯猿记:最近频繁用到一个函数: split()。

关于这个函数,在JS和Java里面都有,下面列举了一些相关使用以及注意事项

导航

用途

JS

Java

​源码(split)


用途

      有时候可能会因为某些需求,需要对字符进行相关处理来获取到自己想要的部分。比如一些文件的路径字符,想要在一长串地址中只获取文件名。

JS

语法:

        stringObject.split(separator,howmany)

  • 第一个参数是必需的,可以是字符串或者正则表达式,会根据这个参数的值,将stringObject以这个值为边界进行分割,返回数组。

  • 第二个参数为可选的,指定返回的数组的最大长度,最终返回的子串不会多于这个最大长度。

这里就是在js中,split的相关使用。这里有两个地方需要注意:

  • 当separator为 “|”时,js不需要进行转义,在Java中则不同。

  • 当使用的第二个参数指定最大长度时,超过本来子串的长度,最终数组的长度还是根据它本身来定。arr3是指定的长度为8,返回的数组是4.这里不要混淆。

Java

语法:stringObj.split(separator, limit)

这两个参数的含义是和js中的一致,这里就不做过多介绍了。

说说几个注意点:

  • Java中当 separator 为 * ^ : | . \   中的字符时,需要用到转义 \\。例如:split("\\|");

  • Java中separator是不能忽略的,因为在string类中只有如下方法:

  

  • 在上面说到js中的第一个参数是必须的,但是我在测试的时候发现不写也不会报错,会返回只包括stringObject一个的数组,在实际中是没有这样的需求的,忽略不计。

      

       这里介绍的只是关于split在js和Java中简单的使用,想要深入理解,建议大家读读Java中的源码。另外,separator参数是可以使用正则表达式的,掌握正则表达式是不可缺少的,后面为大家整理出来。

​源码(split)

    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.
         */
        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))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            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();
            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);
        }
        return Pattern.compile(regex).split(this, limit);
    }


    public String[] split(String regex) {
        return split(regex, 0);
    }

温馨提示

如需转载,请注明原链接,谢谢。

作者博客:https://blog.csdn.net/qq_36377960

猜你喜欢

转载自blog.csdn.net/qq_36377960/article/details/106917176
今日推荐