1. 程式人生 > >StringUtils類常用方法:轉換、移除、替換、反轉

StringUtils類常用方法:轉換、移除、替換、反轉

一、轉換

  StringUtils中涉及大小寫轉換以及判斷字串大小寫的方法如下:

    1)StringUtils.capitalize(String str)
    2)StringUtils.uncapitalize(String str)
    3)StringUtils.upperCase(String str)
    4)StringUtils.upperCase(String str,Locale locale)
    5)StringUtils.lowerCase(String str)
    6)StringUtils.lowerCase(String str,Locale
locale) 7)StringUtils.swapCase(String str) 8)StringUtils.isAllUpperCase(CharSequence cs) 9)StringUtils.isAllLowerCase(CharSequence cs)

  (1)字串首字母大小寫轉換

    StringUtils.capitalize(null)); // null (注意此處不會報異常)
    StringUtils.capitalize("china")); // China (首字母轉大寫)
    StringUtils.uncapitalize(null
)); // null StringUtils.uncapitalize("CHINA")); // cHINA (首字母轉小寫)

  (2)字串整體大小寫轉換

    StringUtils.upperCase(null)); // null
    StringUtils.upperCase("china")); // CHINA (全部轉為大寫)
    StringUtils.upperCase("china", Locale.ENGLISH)); // CHINA (按照指定規則轉換為大寫)
    StringUtils.lowerCase(null)); // null
    StringUtils.lowerCase("CHINA"
)); // china (全部轉換為小寫) StringUtils.lowerCase("CHINA", Locale.ENGLISH)); // china (按照指定轉換規則轉換為小寫)

  (3)字串大小寫互換

    StringUtils.swapCase(null)); // null
    StringUtils.swapCase("chINA")); // CHina

  (4)判斷字串是否全部是大寫或小寫(空或空白符均為false)

    StringUtils.isAllUpperCase(null)); // false
    StringUtils.isAllUpperCase("")); // false
    StringUtils.isAllUpperCase(" ")); // false
    StringUtils.isAllUpperCase("CHINA")); // true
    StringUtils.isAllLowerCase(null)); // false
    StringUtils.isAllLowerCase("")); // false
    StringUtils.isAllLowerCase(" ")); // false
    StringUtils.isAllLowerCase("china")); // true

二、移除

  從字串中移除匹配的字元或字元序列,如果要移除的字元或字元序列在字串中不存在,即無匹配,則不進行移除

    1)StringUtils.remove(String str, char remove)
    2)StringUtils.remove(String str, String remove)
    3)StringUtils.removeStart(String str, String remove)
    4)StringUtils.removeStartIgnoreCase(String str, String remove)
    5)StringUtils.removeEnd(String str, String remove)
    6)StringUtils.removeEndIgnoreCase(String str, String remove)
    7)StringUtils.deleteWhitespace(String str)

  (1)移除單個字元

    StringUtils.remove(null, 'a')); // null (注意此處及下一行為null)
    StringUtils.remove('china', null) // china 
    StringUtils.remove("china", 'i')); // chna
    StringUtils.remove("china", 'b')); // china (如果要移除的字元不存在,則返回原字串)

  (2)移除指定字元序列

    StringUtils.remove("china", "in")); // cha
    StringUtils.remove("china", "nin")); // china

  (3)移除開頭匹配的字元序列

    StringUtils.removeStart("china", "ch")); // ina
    StringUtils.removeStartIgnoreCase("china", "CHI")); // na (忽略大小寫)

  (4)移除結尾匹配的字元序列

    StringUtils.removeEnd("china", "na")); // chi
    StringUtils.removeEndIgnoreCase("china", "NA")); // chi (忽略大小寫)

  (5)移除空白字元

    StringUtils.deleteWhitespace(null)); //null
    StringUtils.deleteWhitespace(" c h  i\tn\ra")); // china

三、替換

  StringUtils中常用的替換方法有如下幾種

    1)replace(String text, String searchString, String replacement)
    2replace(String text, String searchString, String replacement, int max)
    3)replaceChars(String str, char searchChar, char replaceChar)
    4)replaceChars(String str, String searchChars, String replaceChars)
    5)replaceOnce(String text, String searchString, String replacement)
    6)overlay(String str,String overlay,int start,int end)
    7)replaceEach(String text, String[] searchList, String[] replacementList)
    8)replaceEachRepeatedly(String text, String[] searchList, String[]replacementList)

  需要注意的是,若被替換的字串為null,或者被替換的字元或字元序列為null,又或者替換的字元或字元序列為null,那麼此次替換都會被忽略,返回原字串

  (1)替換單個字元或字元序列

    (a)replace方法

      replace方法可以替換單個字元序列

        StringUtils.replace("china", null, "z")); // china (此處被替換字元序列為null,因此替換會被忽略,返回原字串)
        StringUtils.replace("china", "c", null)); // china (此處替換字元序列為null,因此替換也被忽略,返回原字串)
        StringUtils.replace("china", "a", "ese")); // chinese
        StringUtils.replace("china", "a", "")); // chin

      replace方法還可以指定最大替換的個數

        StringUtils.replace("aabaaa", "a", "z", 0)); // aabaaa (0表示替換的個數為0,也就是不替換)
        StringUtils.replace("aabaaa", "a", "z", 1)); // zabaaa (1表示最多替換1個)
        StringUtils.replace("aabaaa", "a", "z", 2)); // zzbaaa (2表示最多替換2個)
        StringUtils.replace("aabaaa", "a", "z", 3)); // zzbzaa (3表示最多替換3個)
        StringUtils.replace("aabaaa", "a", "z", -1)); // zzbzaa (-1表示全部替換)

    (b)replaceChars方法

      replaceChars方法可以替換單個字元或者單個字元序列

        StringUtils.replaceChars("china", 'a', 'z')); // chinz
        StringUtils.replaceChars("china", "a", "z")); // chinz

    (c)replaceOnce方法

      replaceOnce方法只會替換一次,也就是隻會替換第一個要替換的字元序列,後面即使有匹配的字元序列也不會被替換

        StringUtils.replaceOnce("abaa", "a", "z")); // zbaa

    (d)overlay方法

      overlay(String str,String overlay,int start,int end)方法可以在指定位置進行字元序列替換,從start索引處開始(包含)到end-1索引處為止進行替換

         StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzef

      這裡有一些特殊情況:

      1)起始索引start小於結束索引end,這時會將end作為起始索引,start作為結束索引

         StringUtils.overlay("abcdef", "zzzz", 4, 2)); // abzzzzef
         StringUtils.overlay("abcdef", "zzzz", 4, 3)); // abczzzzef
         StringUtils.overlay("abcdef", "zzzz", 4, 4)); // abcdzzzzef
         StringUtils.overlay("abcdef", "zzzz", 4, 5)); // abcdzzzzf

      2)起始索引start為負數,這時start會被當作0處理

         StringUtils.overlay("abcdef", "zzzz", -1, 2)); // abcdzz
         StringUtils.overlay("abcdef", "zzzz", -2, -3)); // zzzzabcdef

      3)結束索引end大於原始字串的長度,這時end會被當作原始字串長度處理

         StringUtils.overlay("abcdef", "zzzz", 8, 10)); // abcdefzzzz

  (2)同時替換多個字元序列

    (a)replaceEach方法

      replaceEach(String text, String[] searchList, String[] replacementList)方法可以同時替換多個字元序列,但被替換和替換的字元序列的個數應該對應,否則會報IllegalArgumentException

         StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" })); // xhinz (將ch和a分別替換為x和z)
         StringUtils.replaceEach("china", null, new String[] { "x", "z" })); // china (存在null,不進行替換)
         StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z", "y" })); // IllegalArgumentException (被替換和替換的個數不對應)
           

    (b)replaceEachRepeatedly方法

      replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)方法可以迴圈進行替換,具體見下面的例子:

           StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "z" })); // zhina (c被替換為x,x又被替換為z)

      但如果替換是一個死迴圈,則會報IllegalStateException:

           StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "c" })); // IllegalStateException (c被替換為x,x又被替換為c)

四、反轉

  StringUtils中有關反轉的方法如下:

    1)reverse(String str)
    2)reverseDelimited(String str, char separatorChar)

  (1)簡單反轉

  reverse(String str)方法

    StringUtils.reverse("china")); // anihc

  (2)根據指定分隔符進行反轉,分隔符之間的字元不進行反轉

    StringUtils.reverseDelimited("china", ',')); // china
    StringUtils.reverseDelimited("cxhinxa", 'x')); // axhinxz
    StringUtils.reverseDelimited("c.hin.a", '.')); // a.hin.c
    StringUtils.reverseDelimited("c.hina", '.')); // hina.c