类 Strings


  • public class Strings
    extends java.lang.Object
    Utils methods for String
    • 构造器概要

      构造器 
      构造器 说明
      Strings()  
    • 方法概要

      所有方法 静态方法 具体方法 已过时的方法 
      修饰符和类型 方法 说明
      static java.lang.String appendIfMissing​(java.lang.String str, java.lang.String suffix)
      If str do not has specific suffix, return a new String appending the suffix; return str its self otherwise.
      static java.lang.String capitalize​(java.lang.String str)
      Return str, with first char is uppercase.
      static int countOf​(java.lang.String str, java.lang.String sub)
      Calculate the count of sub string.
      static int countOf​(java.lang.String str, java.lang.String sub, boolean overlap)
      Calculate the count of sub string.
      static java.lang.String deCapitalize​(java.lang.String str)
      Return str, with first char is lowercase.
      static java.lang.String join​(java.lang.CharSequence prefix, java.lang.CharSequence suffix, java.lang.CharSequence delimiter, java.lang.CharSequence... strings)
      已过时。
      use Joiner
      static java.lang.String join​(java.lang.CharSequence prefix, java.lang.CharSequence suffix, java.lang.CharSequence delimiter, java.lang.Iterable<? extends java.lang.CharSequence> strings)
      已过时。
      use Joiner
      static java.lang.String joinLines​(java.lang.Iterable<? extends java.lang.CharSequence> strings)
      Join string lines with '\n'
      static java.lang.String nullToEmpty​(@Nullable java.lang.String str)
      If str is null, return empty str; else return str self.
      static java.lang.String padLeft​(java.lang.String str, int len, char padding)
      Pad the string, at left, to len, with padding char.
      static java.lang.String padRight​(java.lang.String str, int len, char padding)
      Pad the string, at right, to len, with padding char.
      static java.lang.String padToCenter​(java.lang.String str, int len, char padding)
      Return a centered string of length width.Padding is done using the specified padding character.
      static java.lang.String prependIfMissing​(java.lang.String str, java.lang.String prefix)
      If str do not has specific prefix, return a new String prepending the prefix; return str its self otherwise.
      static java.lang.String removePrefix​(java.lang.String str, int len)
      Remove string prefix with len. if len larger than str size, return empty string.
      static java.lang.String removePrefix​(java.lang.String str, java.lang.String prefix)
      If str start with prefix, remove prefix
      static java.lang.String removePrefixAndSuffix​(java.lang.String str, java.lang.String prefix, java.lang.String suffix)
      If str start with prefix, remove prefix; If str end with suffix, remove suffix; If prefix and suffix overlapped, return empty str.
      static java.lang.String removeSuffix​(java.lang.String str, int len)
      Remove string suffix with len. if len larger than str size, return empty string.
      static java.lang.String removeSuffix​(java.lang.String str, java.lang.String suffix)
      If str end with suffix, remove suffix
      static java.lang.String repeat​(java.lang.String str, int times)
      Return a new string, which content is repeat times of origin str.
      static java.lang.String reverse​(java.lang.String str)
      Return a reversed string
      static java.lang.String subStringAfter​(java.lang.String str, java.lang.String sep)
      Return subString after first matched separator.
      static java.lang.String subStringAfterLast​(java.lang.String str, java.lang.String sep)
      Return subString after last separator.
      static java.lang.String subStringBefore​(java.lang.String str, java.lang.String sep)
      Return subString before first matched separator.
      static java.lang.String subStringBeforeLast​(java.lang.String str, java.lang.String sep)
      Return subString before last matched separator.
      static double toDouble​(java.lang.String str, double defaultValue)
      Parse str to double.
      static float toFloat​(java.lang.String str, float defaultValue)
      Parse str to float.
      static int toInt​(java.lang.String str, int defaultValue)
      Parse str to int.
      static long toLong​(java.lang.String str, long defaultValue)
      Parse str to long.
      static java.lang.String trimPrefix​(java.lang.String str, java.lang.String prefix)
      已过时。
      static java.lang.String trimSuffix​(java.lang.String str, java.lang.String suffix)
      已过时。
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • Strings

        public Strings()
    • 方法详细资料

      • nullToEmpty

        public static java.lang.String nullToEmpty​(@Nullable java.lang.String str)
        If str is null, return empty str; else return str self.
        参数:
        str - the string
        返回:
        nonNull string
      • toInt

        public static int toInt​(java.lang.String str,
                                int defaultValue)
        Parse str to int. If failed, return defaultValue.
      • toLong

        public static long toLong​(java.lang.String str,
                                  long defaultValue)
        Parse str to long. If failed, return defaultValue.
      • toFloat

        public static float toFloat​(java.lang.String str,
                                    float defaultValue)
        Parse str to float. If failed, return defaultValue.
      • toDouble

        public static double toDouble​(java.lang.String str,
                                      double defaultValue)
        Parse str to double. If failed, return defaultValue.
      • join

        @Deprecated
        public static java.lang.String join​(java.lang.CharSequence prefix,
                                            java.lang.CharSequence suffix,
                                            java.lang.CharSequence delimiter,
                                            java.lang.Iterable<? extends java.lang.CharSequence> strings)
        已过时。
        use Joiner
        Join strings, with prefix, suffix, and delimiter
        参数:
        prefix - the prefix to prepend, can not be null
        suffix - the suffix to append, can not be null
        delimiter - the delimiter to join multi string items, can not be null
        strings - the string items
        返回:
        new string
      • join

        @Deprecated
        public static java.lang.String join​(java.lang.CharSequence prefix,
                                            java.lang.CharSequence suffix,
                                            java.lang.CharSequence delimiter,
                                            java.lang.CharSequence... strings)
        已过时。
        use Joiner
        Join strings, with prefix, suffix, and delimiter
        参数:
        prefix - the prefix to prepend, can not be null
        suffix - the suffix to append, can not be null
        delimiter - the delimiter to join multi string items, can not be null
        strings - the string items
        返回:
        new string
      • repeat

        public static java.lang.String repeat​(java.lang.String str,
                                              int times)
        Return a new string, which content is repeat times of origin str.
      • subStringBefore

        public static java.lang.String subStringBefore​(java.lang.String str,
                                                       java.lang.String sep)
        Return subString before first matched separator. If str does not contains sep, return the origin string.
        参数:
        str - the origin str. can not be null
        sep - the separator to get sub string. cannot be null or empty
        返回:
        sub string
      • subStringBeforeLast

        public static java.lang.String subStringBeforeLast​(java.lang.String str,
                                                           java.lang.String sep)
        Return subString before last matched separator. If str does not contains sep, return the origin string.
        参数:
        str - the origin str. can not be null
        sep - the separator to get sub string. cannot be null or empty
        返回:
        sub string
      • subStringAfter

        public static java.lang.String subStringAfter​(java.lang.String str,
                                                      java.lang.String sep)
        Return subString after first matched separator. If str does not contains sep, return the origin string.
        参数:
        str - the origin str. can not be null
        sep - the separator to get sub string. cannot be null or empty
        返回:
        sub string
      • subStringAfterLast

        public static java.lang.String subStringAfterLast​(java.lang.String str,
                                                          java.lang.String sep)
        Return subString after last separator. If str does not contains sep, return the origin string.
        参数:
        str - the origin str. can not be null
        sep - the separator to get sub string. cannot be null or empty
        返回:
        sub string
      • countOf

        public static int countOf​(java.lang.String str,
                                  java.lang.String sub)
        Calculate the count of sub string. The sub string do not overlap.
        参数:
        str - can not be null
        sub - the sub string, can not be null or empty
        返回:
        the sub string occurred count
      • countOf

        public static int countOf​(java.lang.String str,
                                  java.lang.String sub,
                                  boolean overlap)
        Calculate the count of sub string.
        参数:
        str - can not be null
        sub - the sub string, can not be null or empty
        overlap - if the sub found in str can overlap
        返回:
        the sub string occurred count
      • trimSuffix

        @Deprecated
        public static java.lang.String trimSuffix​(java.lang.String str,
                                                  java.lang.String suffix)
        已过时。
      • trimPrefix

        @Deprecated
        public static java.lang.String trimPrefix​(java.lang.String str,
                                                  java.lang.String prefix)
        已过时。
      • removeSuffix

        public static java.lang.String removeSuffix​(java.lang.String str,
                                                    java.lang.String suffix)
        If str end with suffix, remove suffix
        参数:
        str - string
        suffix - suffix
        返回:
        str without suffix
      • removePrefix

        public static java.lang.String removePrefix​(java.lang.String str,
                                                    java.lang.String prefix)
        If str start with prefix, remove prefix
        参数:
        str - string
        prefix - suffix
        返回:
        str without prefix
      • removePrefixAndSuffix

        public static java.lang.String removePrefixAndSuffix​(java.lang.String str,
                                                             java.lang.String prefix,
                                                             java.lang.String suffix)
        If str start with prefix, remove prefix; If str end with suffix, remove suffix; If prefix and suffix overlapped, return empty str.
        参数:
        str - string
        prefix - suffix
        suffix - suffix
        返回:
        str without prefix and suffix
      • removePrefix

        public static java.lang.String removePrefix​(java.lang.String str,
                                                    int len)
        Remove string prefix with len. if len larger than str size, return empty string.
        参数:
        str - the string
        len - the prefix len to remove
        返回:
        string without prefix
      • removeSuffix

        public static java.lang.String removeSuffix​(java.lang.String str,
                                                    int len)
        Remove string suffix with len. if len larger than str size, return empty string.
        参数:
        str - the string
        len - the suffix len to remove
        返回:
        string without suffix
      • capitalize

        public static java.lang.String capitalize​(java.lang.String str)
        Return str, with first char is uppercase. If the first char of original str is already uppercase, or str is empty, return str self. Note: this method do not handle surrogate correctly.
        参数:
        str - the original str
        返回:
        new string with first char uppercase
      • deCapitalize

        public static java.lang.String deCapitalize​(java.lang.String str)
        Return str, with first char is lowercase. If the first char of original str is already lowercase, or str is empty, return str self. Note: this method do not handle surrogate correctly.
        参数:
        str - the original str
        返回:
        new string with first char lowercase
      • padLeft

        public static java.lang.String padLeft​(java.lang.String str,
                                               int len,
                                               char padding)
        Pad the string, at left, to len, with padding char. If str size already equals or larger than len, return string self.
        参数:
        str - the str to be pad
        len - the desired len of padded str
        padding - the padding char
        返回:
        padded str
      • padRight

        public static java.lang.String padRight​(java.lang.String str,
                                                int len,
                                                char padding)
        Pad the string, at right, to len, with padding char. If str size already equals or larger than len, return string self.
        参数:
        str - the str to be pad
        len - the desired len of padded str
        padding - the padding char
        返回:
        padded str
      • padToCenter

        public static java.lang.String padToCenter​(java.lang.String str,
                                                   int len,
                                                   char padding)
        Return a centered string of length width.Padding is done using the specified padding character.
        参数:
        str - the str to be pad
        len - the desired len of padded str
        padding - the padding char
        返回:
        padded str
      • joinLines

        public static java.lang.String joinLines​(java.lang.Iterable<? extends java.lang.CharSequence> strings)
        Join string lines with '\n'
      • prependIfMissing

        public static java.lang.String prependIfMissing​(java.lang.String str,
                                                        java.lang.String prefix)
        If str do not has specific prefix, return a new String prepending the prefix; return str its self otherwise.
      • appendIfMissing

        public static java.lang.String appendIfMissing​(java.lang.String str,
                                                       java.lang.String suffix)
        If str do not has specific suffix, return a new String appending the suffix; return str its self otherwise.
      • reverse

        public static java.lang.String reverse​(java.lang.String str)
        Return a reversed string
        参数:
        str - the original string
        返回:
        a reversed string