Class Strings

java.lang.Object
org.elasticsearch.common.Strings

public class Strings extends Object
  • Field Details

    • EMPTY_ARRAY

      public static final String[] EMPTY_ARRAY
    • INVALID_FILENAME_CHARS

      public static final Set<Character> INVALID_FILENAME_CHARS
  • Method Details

    • spaceify

      public static void spaceify(int spaces, String from, StringBuilder to) throws Exception
      Throws:
      Exception
    • splitSmart

      public static List<String> splitSmart(String s, String separator, boolean decode)
      Splits a backslash escaped string on the separator.

      Current backslash escaping supported:
      \n \t \r \b \f are escaped the same as a Java String
      Other characters following a backslash are produced verbatim (\c => c)

      Parameters:
      s - the string to split
      separator - the separator to split on
      decode - decode backslash escaping
    • hasLength

      public static boolean hasLength(CharSequence str)
      Check that the given CharSequence is neither null nor of length 0. Note: Will return true for a CharSequence that purely consists of whitespace.
       StringUtils.hasLength(null) = false
       StringUtils.hasLength("") = false
       StringUtils.hasLength(" ") = true
       StringUtils.hasLength("Hello") = true
       
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null and has length
      See Also:
    • hasLength

      public static boolean hasLength(BytesReference bytesReference)
      Check that the given BytesReference is neither null nor of length 0 Note: Will return true for a BytesReference that purely consists of whitespace.
      Parameters:
      bytesReference - the BytesReference to check (may be null)
      Returns:
      true if the BytesReference is not null and has length
      See Also:
    • hasLength

      public static boolean hasLength(String str)
      Check that the given String is neither null nor of length 0. Note: Will return true for a String that purely consists of whitespace.
      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null and has length
      See Also:
    • isEmpty

      public static boolean isEmpty(CharSequence str)
      Check that the given CharSequence is either null or of length 0. Note: Will return false for a CharSequence that purely consists of whitespace.
       StringUtils.isEmpty(null) = true
       StringUtils.isEmpty("") = true
       StringUtils.isEmpty(" ") = false
       StringUtils.isEmpty("Hello") = false
       
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is either null or has a zero length
    • hasText

      public static boolean hasText(CharSequence str)
      Check whether the given CharSequence has actual text. More specifically, returns true if the string not null, its length is greater than 0, and it contains at least one non-whitespace character.
       StringUtils.hasText(null) = false
       StringUtils.hasText("") = false
       StringUtils.hasText(" ") = false
       StringUtils.hasText("12345") = true
       StringUtils.hasText(" 12345 ") = true
       
      Parameters:
      str - the CharSequence to check (may be null)
      Returns:
      true if the CharSequence is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • hasText

      public static boolean hasText(String str)
      Check whether the given String has actual text. More specifically, returns true if the string not null, its length is greater than 0, and it contains at least one non-whitespace character.
      Parameters:
      str - the String to check (may be null)
      Returns:
      true if the String is not null, its length is greater than 0, and it does not contain whitespace only
      See Also:
    • trimLeadingCharacter

      public static String trimLeadingCharacter(String str, char leadingCharacter)
      Trim all occurrences of the supplied leading character from the given String.
      Parameters:
      str - the String to check
      leadingCharacter - the leading character to be trimmed
      Returns:
      the trimmed String
    • substringMatch

      public static boolean substringMatch(CharSequence str, int index, CharSequence substring)
      Test whether the given string matches the given substring at the given index.
      Parameters:
      str - the original string (or StringBuilder)
      index - the index in the original string to start matching against
      substring - the substring to match at the given index
    • replace

      public static String replace(String inString, String oldPattern, String newPattern)
      Replace all occurrences of a substring within a string with another string.
      Parameters:
      inString - String to examine
      oldPattern - String to replace
      newPattern - String to insert
      Returns:
      a String with the replacements
    • delete

      public static String delete(String inString, String pattern)
      Delete all occurrences of the given substring.
      Parameters:
      inString - the original String
      pattern - the pattern to delete all occurrences of
      Returns:
      the resulting String
    • deleteAny

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

      public static String quote(String str)
      Quote the given String with single quotes.
      Parameters:
      str - the input String (e.g. "myString")
      Returns:
      the quoted String (e.g. "'myString'"), or null if the input was null
    • 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.
      Parameters:
      str - the String to capitalize, may be null
      Returns:
      the capitalized String, null if null
    • validFileName

      public static boolean validFileName(String fileName)
    • validFileNameExcludingAstrix

      public static boolean validFileNameExcludingAstrix(String fileName)
    • toStringArray

      public static String[] toStringArray(Collection<String> collection)
      Copy the given Collection into a String array. The Collection must contain String elements only.
      Parameters:
      collection - the Collection to copy
      Returns:
      the String array (null if the passed-in Collection was null)
    • concatStringArrays

      public static String[] concatStringArrays(String[] first, String[] second)
      Concatenate two string arrays into a third
    • tokenizeByCommaToSet

      public static Set<String> tokenizeByCommaToSet(String s)
      Tokenize the specified string by commas to a set, trimming whitespace and ignoring empty tokens.
      Parameters:
      s - the string to tokenize
      Returns:
      the set of tokens
    • splitStringByCommaToArray

      public static String[] splitStringByCommaToArray(String s)
      Split the specified string by commas to an array.
      Parameters:
      s - the string to split
      Returns:
      the array of split values
      See Also:
    • split

      public static String[] split(String toSplit, String delimiter)
      Split a String at the first occurrence of the delimiter. Does not include the delimiter in the result.
      Parameters:
      toSplit - the string to split
      delimiter - to split the string up with
      Returns:
      a two element array with index 0 being before the delimiter, and index 1 being after the delimiter (neither element includes the delimiter); or null if the delimiter wasn't found in the given input String
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String s, String delimiters)
      Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.

      The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

      Parameters:
      s - the String to tokenize
      delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).
      Returns:
      an array of the tokens
      See Also:
    • delimitedListToStringArray

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

      A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

      Parameters:
      str - the input String
      delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
      Returns:
      an array of the tokens in the list
      See Also:
    • delimitedListToStringArray

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

      A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

      Parameters:
      str - the input String
      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.
      Returns:
      an array of the tokens in the list
      See Also:
    • commaDelimitedListToStringArray

      public static String[] commaDelimitedListToStringArray(String str)
      Convert a CSV list into an array of Strings.
      Parameters:
      str - the input String
      Returns:
      an array of Strings, or the empty array in case of empty input
    • commaDelimitedListToSet

      public static Set<String> commaDelimitedListToSet(String str)
      Convenience method to convert a CSV string list to a set. Note that this will suppress duplicates.
      Parameters:
      str - the input String
      Returns:
      a Set of String entries in the list
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Iterable<?> coll, String delim, String prefix, String suffix)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - the Collection to display
      delim - the delimiter to use (probably a ",")
      prefix - the String to start each element with
      suffix - the String to end each element with
      Returns:
      the delimited String
    • collectionToDelimitedString

      public static void collectionToDelimitedString(Iterable<?> coll, String delim, String prefix, String suffix, StringBuilder sb)
    • collectionToDelimitedStringWithLimit

      public static void collectionToDelimitedStringWithLimit(Iterable<?> coll, String delim, String prefix, String suffix, int appendLimit, StringBuilder sb)
      Converts a collection of items to a string like collectionToDelimitedString(Iterable, String, String, String, StringBuilder) except that it stops if the string gets too long and just indicates how many items were omitted.
      Parameters:
      coll - the collection of items to display
      delim - the delimiter to write between the items (usually ",")
      prefix - a string to write before each item (usually "" or "[")
      suffix - a string to write after each item (usually "" or "]")
      appendLimit - if this many characters have been appended to the string and there are still items to display then the remaining items are omitted
    • collectionToDelimitedString

      public static String collectionToDelimitedString(Iterable<?> coll, String delim)
      Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      coll - the Collection to display
      delim - the delimiter to use (probably a ",")
      Returns:
      the delimited String
    • collectionToCommaDelimitedString

      public static String collectionToCommaDelimitedString(Iterable<?> coll)
      Convenience method to return a Collection as a CSV String. E.g. useful for toString() implementations.
      Parameters:
      coll - the Collection to display
      Returns:
      the delimited String
    • arrayToDelimitedString

      public static String arrayToDelimitedString(Object[] arr, String delim)
      Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
      Parameters:
      arr - the array to display
      delim - the delimiter to use (probably a ",")
      Returns:
      the delimited String
    • arrayToDelimitedString

      public static void arrayToDelimitedString(Object[] arr, String delim, StringBuilder sb)
    • arrayToCommaDelimitedString

      public static String arrayToCommaDelimitedString(Object[] arr)
      Convenience method to return a String array as a CSV String. E.g. useful for toString() implementations.
      Parameters:
      arr - the array to display
      Returns:
      the delimited String
    • format1Decimals

      public static String format1Decimals(double value, String suffix)
      Format the double value with a single decimal points, trimming trailing '.0'.
    • toUTF8Bytes

      public static byte[] toUTF8Bytes(CharSequence charSequence)
    • toUTF8Bytes

      public static byte[] toUTF8Bytes(CharSequence charSequence, org.apache.lucene.util.BytesRefBuilder spare)
    • substring

      public static String substring(String s, int beginIndex, int endIndex)
      Return substring(beginIndex, endIndex) that is impervious to string length.
    • isAllOrWildcard

      public static boolean isAllOrWildcard(String[] data)
      If an array only consists of zero or one element, which is "*" or "_all" return an empty array which is usually used as everything
    • isAllOrWildcard

      public static boolean isAllOrWildcard(String data)
      Returns `true` if the string is `_all` or `*`.
    • toString

      public static String toString(org.elasticsearch.xcontent.ToXContent toXContent)
      Return a String that is the json representation of the provided ToXContent. Wraps the output into an anonymous object if needed. The content is not pretty-printed nor human readable.
    • toString

      public static String toString(org.elasticsearch.xcontent.ToXContent toXContent, org.elasticsearch.xcontent.ToXContent.Params params)
      Return a String that is the json representation of the provided ToXContent. Wraps the output into an anonymous object if needed. Allows to configure the params. The content is not pretty-printed nor human readable.
    • toString

      public static String toString(org.elasticsearch.xcontent.XContentBuilder xContentBuilder)
      Returns a string representation of the builder (only applicable for text based xcontent).
      Parameters:
      xContentBuilder - builder containing an object to converted to a string
    • toString

      public static String toString(org.elasticsearch.xcontent.ToXContent toXContent, boolean pretty, boolean human)
      Return a String that is the json representation of the provided ToXContent. Wraps the output into an anonymous object if needed. Allows to control whether the outputted json needs to be pretty printed and human readable.
    • cleanTruncate

      public static String cleanTruncate(String s, int length)
      Truncates string to a length less than length. Backtracks to throw out high surrogates.
    • requireNonEmpty

      public static String requireNonEmpty(String str, String message)
      Checks that the supplied string is neither null nor empty, per isNullOrEmpty(String). If this check fails, then an IllegalArgumentException is thrown with the supplied message.
      Parameters:
      str - the String to check
      message - the exception message to use if str is null or empty
      Returns:
      the supplied str
    • isNullOrEmpty

      public static boolean isNullOrEmpty(@Nullable String s)
    • coalesceToEmpty

      public static String coalesceToEmpty(@Nullable String s)
    • padStart

      public static String padStart(String s, int minimumLength, char c)
    • toLowercaseAscii

      public static String toLowercaseAscii(String in)