类 StringUtils

java.lang.Object
com.alibaba.nacos.common.utils.StringUtils

public class StringUtils extends Object
string util.
作者:
Nacos, zzq
  • 字段详细资料

  • 方法详细资料

    • newStringForUtf8

      public static String newStringForUtf8(byte[] bytes)

      Create a string with encoding format as utf8.

      参数:
      bytes - the bytes that make up the string
      返回:
      created string
    • isBlank

      public static boolean isBlank(CharSequence cs)

      Checks if a string is empty (""), null and whitespace only.

      参数:
      cs - the string to check
      返回:
      true if the string is empty and null and whitespace
    • isNotBlank

      public static boolean isNotBlank(String str)

      Checks if a string is not empty (""), not null and not whitespace only.

      参数:
      str - the string to check, may be null
      返回:
      true if the string is not empty and not null and not whitespace
    • isNotEmpty

      public static boolean isNotEmpty(String str)

      Checks if a str is not empty ("") or not null.

      参数:
      str - the str to check, may be null
      返回:
      true if the str is not empty or not null
    • isEmpty

      public static boolean isEmpty(String str)

      Checks if a str is empty ("") or null.

      参数:
      str - the str to check, may be null
      返回:
      true if the str is empty or null
    • defaultIfEmpty

      public static String defaultIfEmpty(String str, String defaultStr)

      Returns either the passed in CharSequence, or if the CharSequence is empty or null, the value of defaultStr.

      参数:
      str - the CharSequence to check, may be null
      defaultStr - the default CharSequence to return if the input is empty ("") or null, may be null
      返回:
      the passed in CharSequence, or the default
    • defaultIfBlank

      public static String defaultIfBlank(String str, String defaultStr)

      Returns either the passed in CharSequence, or if the CharSequence is empty or null or whitespace only, the value of defaultStr.

      参数:
      str - the CharSequence to check, may be null, may be whitespace only
      defaultStr - the default CharSequence to return if the input is empty ("") or null, may be null
      返回:
      the passed in CharSequence, or the default
    • defaultEmptyIfBlank

      public static String defaultEmptyIfBlank(String str)

      Returns either the passed in CharSequence, or if the CharSequence is empty or null or whitespace only, the value of EmptyString.

      参数:
      str - the CharSequence to check, may be null, may be whitespace only
      返回:
      the passed in CharSequence, or the empty string
    • equals

      public static boolean equals(String str1, String str2)

      Compares two CharSequences, returning true if they represent equal sequences of characters.

      参数:
      str1 - the first string, may be null
      str2 - the second string, may be null
      返回:
      true if the string are equal (case-sensitive), or both null
      另请参阅:
    • trim

      public static String trim(String str)

      Removes control characters (char <= 32) from both ends of this String, handling null by returning null.

      参数:
      str - the String to be trimmed, may be null
      返回:
      the trimmed string, null if null String input
    • substringBetween

      public static String substringBetween(String str, String open, String close)
      Substring between two index.
      参数:
      str - string
      open - start index to sub
      close - end index to sub
      返回:
      substring
    • join

      public static String join(Collection collection, String separator)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      参数:
      collection - the Collection of values to join together, may be null
      separator - the separator string to use
      返回:
      the joined String, null if null array input
    • containsIgnoreCase

      public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr)
      Checks if CharSequence contains a search CharSequence irrespective of case, handling null. Case-insensitivity is defined as by String.equalsIgnoreCase(String).

      A null CharSequence will return false.

      参数:
      str - the CharSequence to check, may be null
      searchStr - the CharSequence to find, may be null
      返回:
      true if the CharSequence contains the search CharSequence irrespective of case or false if not or null string input
    • contains

      public static boolean contains(CharSequence str, CharSequence searchStr)
      Checks if CharSequence contains a search CharSequence.
      参数:
      str - the CharSequence to check, may be null
      searchStr - the CharSequence to find, may be null
      返回:
      true if the CharSequence contains the search CharSequence
    • isNoneBlank

      public static boolean isNoneBlank(CharSequence... css)

      Checks if none of the CharSequences are blank ("") or null and whitespace only..

      参数:
      css - the CharSequences to check, may be null or empty
      返回:
      true if none of the CharSequences are blank or null or whitespace only
    • isAnyBlank

      public static boolean isAnyBlank(CharSequence... css)

      Checks if any one of the CharSequences are blank ("") or null and not whitespace only..

      参数:
      css - the CharSequences to check, may be null or empty
      返回:
      true if any of the CharSequences are blank or null or whitespace only
    • startsWith

      public static boolean startsWith(CharSequence str, CharSequence prefix)

      Check if a CharSequence starts with a specified prefix.

      nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

      参数:
      str - the CharSequence to check, may be null
      prefix - the prefix to find, may be null
      返回:
      true if the CharSequence starts with the prefix, case sensitive, or both null
      另请参阅:
    • startsWithIgnoreCase

      public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix)

      Case insensitive check if a CharSequence starts with a specified prefix.

      nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case insensitive.

      参数:
      str - the CharSequence to check, may be null
      prefix - the prefix to find, may be null
      返回:
      true if the CharSequence starts with the prefix, case insensitive, or both null
      另请参阅:
    • deleteWhitespace

      public static String deleteWhitespace(String str)

      Deletes all whitespaces from a String as defined by Character.isWhitespace(char).

      参数:
      str - the String to delete whitespace from, may be null
      返回:
      the String without whitespaces, null if null String input
    • equalsIgnoreCase

      public static boolean equalsIgnoreCase(String str1, String str2)

      Compares two CharSequences, returning true if they represent equal sequences of characters, ignoring case.

      参数:
      str1 - the first string, may be null
      str2 - the second string, may be null
      返回:
      true if the string are equal, case insensitive, or both null
    • split

      public static String[] split(String str, String separatorChars)
      Splits the provided text into an array with a maximum length, separators specified. If separatorChars is empty, divide by blank.
      参数:
      str - the String to parse, may be null
      返回:
      an array of parsed Strings
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a StringTokenizer.

      The given delimiters string can consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character;

      参数:
      str - the String to tokenize (potentially null or empty)
      delimiters - the delimiter characters, assembled as a String (each of the characters is individually considered as a delimiter)
      trimTokens - trim the tokens via String.trim()
      ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
      返回:
      an array of the tokens
      另请参阅:
    • toStringArray

      public static String[] toStringArray(Collection<String> collection)
      Copy the given Collection into a String array.

      The Collection must contain String elements only.

      参数:
      collection - the Collection to copy (potentially null or empty)
      返回:
      the resulting String array
    • hasText

      public static boolean hasText(String str)
      Check whether the given String contains actual text.

      More specifically, this method returns true if the String is not null, its length is greater than 0, and it contains at least one non-whitespace character.

      参数:
      str - the String to check (maybe null)
      返回:
      true if the String is not null, its length is greater than 0, and it does not contain whitespace only
      另请参阅:
    • cleanPath

      public static String cleanPath(String path)
      Normalize the path by suppressing sequences like "path/.." and inner simple dots.

      The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.

      NOTE that cleanPath should not be depended upon in a security context. Other mechanisms should be used to prevent path-traversal issues.

      参数:
      path - the original path
      返回:
      the normalized path
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<?> coll, String delim)
      Convert a Collection into a delimited String (e.g. CSV).

      Useful for toString() implementations.

      参数:
      coll - the Collection to convert (potentially null or empty)
      delim - the delimiter to use (typically a ",")
      返回:
      the delimited String
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
      Convert a Collection to a delimited String (e.g. CSV).

      Useful for toString() implementations.

      参数:
      coll - the Collection to convert (potentially null or empty)
      delim - the delimiter to use (typically a ",")
      prefix - the String to start each element with
      suffix - the String to end each element with
      返回:
      the delimited String
    • hasLength

      public static boolean hasLength(String str)
      Check that the given String is neither null nor of length 0.

      Note: this method returns true for a String that purely consists of whitespace.

      参数:
      str - the String to check (maybe null)
      返回:
      true if the String is not null and has length
      另请参阅:
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter)
      Take a String that is a delimited list and convert it into a String array.

      A single delimiter may consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters, in contrast to tokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean).

      参数:
      str - the input String (potentially null or empty)
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      返回:
      an array of the tokens in the list
      另请参阅:
    • delimitedListToStringArray

      public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
      Take a String that is a delimited list and convert it into a String array.

      A single delimiter may consist of more than one character, but it will still be considered as a single delimiter string, rather than as a bunch of potential delimiter characters, in contrast to tokenizeToStringArray(java.lang.String, java.lang.String, boolean, boolean).

      参数:
      str - the input String (potentially null or empty)
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      charsToDelete - a set of characters to delete; useful for deleting unwanted line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String
      返回:
      an array of the tokens in the list
      另请参阅:
    • deleteAny

      public static String deleteAny(String inString, String charsToDelete)
      Delete any character in a given String.
      参数:
      inString - the original String
      charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
      返回:
      the resulting String
    • replace

      public static String replace(String inString, String oldPattern, String newPattern)
      Replace all occurrences of a substring within a string with another string.
      参数:
      inString - String to examine
      oldPattern - String to replace
      newPattern - String to insert
      返回:
      a String with the replacements
    • applyRelativePath

      public static String applyRelativePath(String path, String relativePath)
      Apply the given relative path to the given Java resource path, assuming standard Java folder separation (i.e. "/" separators).
      参数:
      path - the path to start from (usually a full file path)
      relativePath - the relative path to apply (relative to the full file path above)
      返回:
      the full file path that results from applying the relative path
    • getFilename

      public static String getFilename(String path)
      Extract the filename from the given Java resource path, e.g. "myPath/myFile.txt" &rarr; "myFile.txt".
      参数:
      path - the file path (maybe null)
      返回:
      the extracted filename, or null if none
    • capitalize

      public static String capitalize(String str)
      Capitalize a String, changing the first letter to upper case as per Character.toUpperCase(char). No other letters are changed.
      参数:
      str - the String to capitalize
      返回:
      the capitalized String