类 StringUtils


  • public final class StringUtils
    extends Object
    StringUtils
    • 字段详细资料

      • INDEX_NOT_FOUND

        public static final int INDEX_NOT_FOUND
        另请参阅:
        常量字段值
      • EMPTY_STRING_ARRAY

        public static final String[] EMPTY_STRING_ARRAY
      • EQUAL_CHAR

        public static final char EQUAL_CHAR
        从以下版本开始:
        2.7.5
        另请参阅:
        常量字段值
      • EQUAL

        public static final String EQUAL
      • AND_CHAR

        public static final char AND_CHAR
        另请参阅:
        常量字段值
      • AND

        public static final String AND
      • SEMICOLON_CHAR

        public static final char SEMICOLON_CHAR
        另请参阅:
        常量字段值
      • SEMICOLON

        public static final String SEMICOLON
      • QUESTION_MASK_CHAR

        public static final char QUESTION_MASK_CHAR
        另请参阅:
        常量字段值
      • QUESTION_MASK

        public static final String QUESTION_MASK
      • SLASH_CHAR

        public static final char SLASH_CHAR
        另请参阅:
        常量字段值
      • SLASH

        public static final String SLASH
      • HYPHEN_CHAR

        public static final char HYPHEN_CHAR
        另请参阅:
        常量字段值
      • HYPHEN

        public static final String HYPHEN
    • 方法详细资料

      • length

        public static int length​(CharSequence cs)
        Gets a CharSequence length or 0 if the CharSequence is null.
        参数:
        cs - a CharSequence or null
        返回:
        CharSequence length or 0 if the CharSequence is null.
      • repeat

        public static String repeat​(String str,
                                    int repeat)

        Repeat a String repeat times to form a new String.

         StringUtils.repeat(null, 2) = null
         StringUtils.repeat("", 0)   = ""
         StringUtils.repeat("", 2)   = ""
         StringUtils.repeat("a", 3)  = "aaa"
         StringUtils.repeat("ab", 2) = "abab"
         StringUtils.repeat("a", -2) = ""
         
        参数:
        str - the String to repeat, may be null
        repeat - number of times to repeat str, negative treated as zero
        返回:
        a new String consisting of the original String repeated, null if null String input
      • repeat

        public static String repeat​(String str,
                                    String separator,
                                    int repeat)

        Repeat a String repeat times to form a new String, with a String separator injected each time.

         StringUtils.repeat(null, null, 2) = null
         StringUtils.repeat(null, "x", 2)  = null
         StringUtils.repeat("", null, 0)   = ""
         StringUtils.repeat("", "", 2)     = ""
         StringUtils.repeat("", "x", 3)    = "xxx"
         StringUtils.repeat("?", ", ", 3)  = "?, ?, ?"
         
        参数:
        str - the String to repeat, may be null
        separator - the String to inject, may be null
        repeat - number of times to repeat str, negative treated as zero
        返回:
        a new String consisting of the original String repeated, null if null String input
        从以下版本开始:
        2.5
      • removeEnd

        public static String removeEnd​(String str,
                                       String remove)

        Removes a substring only if it is at the end of a source string, otherwise returns the source string.

        A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string.

         StringUtils.removeEnd(null, *)      = null
         StringUtils.removeEnd("", *)        = ""
         StringUtils.removeEnd(*, null)      = *
         StringUtils.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
         StringUtils.removeEnd("www.domain.com", ".com")   = "www.domain"
         StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
         StringUtils.removeEnd("abc", "")    = "abc"
         
        参数:
        str - the source String to search, may be null
        remove - the String to search for and remove, may be null
        返回:
        the substring with the string removed if found, null if null String input
      • repeat

        public static String repeat​(char ch,
                                    int repeat)

        Returns padding using the specified delimiter repeated to a given length.

         StringUtils.repeat('e', 0)  = ""
         StringUtils.repeat('e', 3)  = "eee"
         StringUtils.repeat('e', -2) = ""
         

        Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.

        参数:
        ch - character to repeat
        repeat - number of times to repeat char, negative treated as zero
        返回:
        String with repeated character
        另请参阅:
        repeat(String, int)
      • stripEnd

        public static String stripEnd​(String str,
                                      String stripChars)

        Strips any of a set of characters from the end of a String.

        A null input String returns null. An empty string ("") input returns the empty string.

        If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

         StringUtils.stripEnd(null, *)          = null
         StringUtils.stripEnd("", *)            = ""
         StringUtils.stripEnd("abc", "")        = "abc"
         StringUtils.stripEnd("abc", null)      = "abc"
         StringUtils.stripEnd("  abc", null)    = "  abc"
         StringUtils.stripEnd("abc  ", null)    = "abc"
         StringUtils.stripEnd(" abc ", null)    = " abc"
         StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
         StringUtils.stripEnd("120.00", ".0")   = "12"
         
        参数:
        str - the String to remove characters from, may be null
        stripChars - the set of characters to remove, null treated as whitespace
        返回:
        the stripped String, null if null String input
      • replace

        public static String replace​(String text,
                                     String searchString,
                                     String replacement)

        Replaces all occurrences of a String within another String.

        A null reference passed to this method is a no-op.

         StringUtils.replace(null, *, *)        = null
         StringUtils.replace("", *, *)          = ""
         StringUtils.replace("any", null, *)    = "any"
         StringUtils.replace("any", *, null)    = "any"
         StringUtils.replace("any", "", *)      = "any"
         StringUtils.replace("aba", "a", null)  = "aba"
         StringUtils.replace("aba", "a", "")    = "b"
         StringUtils.replace("aba", "a", "z")   = "zbz"
         
        参数:
        text - text to search and replace in, may be null
        searchString - the String to search for, may be null
        replacement - the String to replace it with, may be null
        返回:
        the text with any replacements processed, null if null String input
        另请参阅:
        replace(String text, String searchString, String replacement, int max)
      • replace

        public static String replace​(String text,
                                     String searchString,
                                     String replacement,
                                     int max)

        Replaces a String with another String inside a larger String, for the first max values of the search String.

        A null reference passed to this method is a no-op.

         StringUtils.replace(null, *, *, *)         = null
         StringUtils.replace("", *, *, *)           = ""
         StringUtils.replace("any", null, *, *)     = "any"
         StringUtils.replace("any", *, null, *)     = "any"
         StringUtils.replace("any", "", *, *)       = "any"
         StringUtils.replace("any", *, *, 0)        = "any"
         StringUtils.replace("abaa", "a", null, -1) = "abaa"
         StringUtils.replace("abaa", "a", "", -1)   = "b"
         StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
         StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
         StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
         StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
         
        参数:
        text - text to search and replace in, may be null
        searchString - the String to search for, may be null
        replacement - the String to replace it with, may be null
        max - maximum number of values to replace, or -1 if no maximum
        返回:
        the text with any replacements processed, null if null String input
      • isBlank

        public static boolean isBlank​(CharSequence cs)
      • isEmpty

        public static boolean isEmpty​(String str)
        is empty string.
        参数:
        str - source string.
        返回:
        is empty.
      • isNoneEmpty

        public static boolean isNoneEmpty​(String... ss)

        Checks if the strings contain empty or null elements.

         StringUtils.isNoneEmpty(null)            = false
         StringUtils.isNoneEmpty("")              = false
         StringUtils.isNoneEmpty(" ")             = true
         StringUtils.isNoneEmpty("abc")           = true
         StringUtils.isNoneEmpty("abc", "def")    = true
         StringUtils.isNoneEmpty("abc", null)     = false
         StringUtils.isNoneEmpty("abc", "")       = false
         StringUtils.isNoneEmpty("abc", " ")      = true
         
        参数:
        ss - the strings to check
        返回:
        true if all strings are not empty or null
      • isAnyEmpty

        public static boolean isAnyEmpty​(String... ss)

        Checks if the strings contain at least on empty or null element.

         StringUtils.isAnyEmpty(null)            = true
         StringUtils.isAnyEmpty("")              = true
         StringUtils.isAnyEmpty(" ")             = false
         StringUtils.isAnyEmpty("abc")           = false
         StringUtils.isAnyEmpty("abc", "def")    = false
         StringUtils.isAnyEmpty("abc", null)     = true
         StringUtils.isAnyEmpty("abc", "")       = true
         StringUtils.isAnyEmpty("abc", " ")      = false
         
        参数:
        ss - the strings to check
        返回:
        true if at least one in the strings is empty or null
      • isNotEmpty

        public static boolean isNotEmpty​(String str)
        is not empty string.
        参数:
        str - source string.
        返回:
        is not empty.
      • isEquals

        public static boolean isEquals​(String s1,
                                       String s2)
        参数:
        s1 -
        s2 -
        返回:
        equals
      • isInteger

        public static boolean isInteger​(String str)
        is integer string.
        参数:
        str -
        返回:
        is integer
      • parseInteger

        public static int parseInteger​(String str)
      • isJavaIdentifier

        public static boolean isJavaIdentifier​(String s)
        Returns true if s is a legal Java identifier.

        more info.

      • isContains

        public static boolean isContains​(String values,
                                         String value)
      • isContains

        public static boolean isContains​(String str,
                                         char ch)
      • isNotContains

        public static boolean isNotContains​(String str,
                                            char ch)
      • isContains

        public static boolean isContains​(String[] values,
                                         String value)
        参数:
        values -
        value -
        返回:
        contains
      • isNumeric

        public static boolean isNumeric​(String str,
                                        boolean allowDot)
      • toString

        public static String toString​(Throwable e)
        参数:
        e -
        返回:
        string
      • toString

        public static String toString​(String msg,
                                      Throwable e)
        参数:
        msg -
        e -
        返回:
        string
      • translate

        public static String translate​(String src,
                                       String from,
                                       String to)
        translate.
        参数:
        src - source string.
        from - src char table.
        to - target char table.
        返回:
        String.
      • split

        public static String[] split​(String str,
                                     char ch)
        split.
        参数:
        ch - char.
        返回:
        string array.
      • splitToList

        public static List<String> splitToList​(String str,
                                               char ch)
        Splits String around matches of the given character.

        Note: Compare with split(String, char), this method reduce memory copy.

      • splitToSet

        public static Set<String> splitToSet​(String value,
                                             char separatorChar)
        Split the specified value to be a Set
        参数:
        value - the content to be split
        separatorChar - a char to separate
        返回:
        non-null read-only Set
        从以下版本开始:
        2.7.8
      • splitToSet

        public static Set<String> splitToSet​(String value,
                                             char separatorChar,
                                             boolean trimElements)
        Split the specified value to be a Set
        参数:
        value - the content to be split
        separatorChar - a char to separate
        trimElements - require to trim the elements or not
        返回:
        non-null read-only Set
        从以下版本开始:
        2.7.8
      • join

        public static String join​(String[] array)
        join string.
        参数:
        array - String array.
        返回:
        String.
      • join

        public static String join​(String[] array,
                                  char split)
        join string like javascript.
        参数:
        array - String array.
        split - split
        返回:
        String.
      • join

        public static String join​(String[] array,
                                  String split)
        join string like javascript.
        参数:
        array - String array.
        split - split
        返回:
        String.
      • getQueryStringValue

        public static String getQueryStringValue​(String qs,
                                                 String key)
      • parseQueryString

        public static Map<String,​String> parseQueryString​(String qs)
        parse query string to Parameters.
        参数:
        qs - query string.
        返回:
        Parameters instance.
      • camelToSplitName

        public static String camelToSplitName​(String camelName,
                                              String split)
      • toArgumentString

        public static String toArgumentString​(Object[] args)
      • toOSStyleKey

        public static String toOSStyleKey​(String key)
      • isAllUpperCase

        public static boolean isAllUpperCase​(String str)
      • delimitedListToStringArray

        public static String[] delimitedListToStringArray​(String str,
                                                          String delimiter)
      • delimitedListToStringArray

        public static String[] delimitedListToStringArray​(String str,
                                                          String delimiter,
                                                          String charsToDelete)
      • arrayToDelimitedString

        public static String arrayToDelimitedString​(Object[] arr,
                                                    String delim)
      • nullSafeToString

        public static String nullSafeToString​(Object obj)
      • parseParameters

        public static Map<String,​String> parseParameters​(String rawParameters)
        参数:
        rawParameters - format like '[{a:b},{c:d}]'
        返回:
      • decodeHexNibble

        public static int decodeHexNibble​(char c)
      • decodeHexByte

        public static byte decodeHexByte​(CharSequence s,
                                         int pos)
        Decode a 2-digit hex byte from within a string.
      • toCommaDelimitedString

        public static String toCommaDelimitedString​(String one,
                                                    String... others)
        Create the common-delimited String by one or more String members
        参数:
        one - one String
        others - others String
        返回:
        null if one or others is null
        从以下版本开始:
        2.7.8