【常用函數】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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章