Class StringUtils

java.lang.Object
io.micronaut.core.util.StringUtils

public final class StringUtils extends Object
Utility methods for Strings.
Since:
1.0
Author:
Graeme Rocher
  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(@Nullable @Nullable CharSequence str)
      Return whether the given string is empty.
      Parameters:
      str - The string
      Returns:
      True if str is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable @Nullable CharSequence str)
      Return whether the given string is not empty.
      Parameters:
      str - The string
      Returns:
      True if str is not null and not empty
    • hasText

      public static boolean hasText(@Nullable @Nullable CharSequence str)
      Return whether the given string has non whitespace characters.
      Parameters:
      str - The string
      Returns:
      True if str contains any non whitespace characters
    • internListOf

      public static List<String> internListOf(Object... objects)
      Converts the given objects into a set of interned strings contained within an internal pool of sets. See String.intern().
      Parameters:
      objects - The objects
      Returns:
      A unmodifiable, pooled set of strings
    • internMapOf

      public static Map<String,Object> internMapOf(Object... values)
      Converts the given objects into a map of interned strings. See String.intern().
      Parameters:
      values - The objects
      Returns:
      An unmodifiable set of strings
      See Also:
    • isDigits

      public static boolean isDigits(String str)
      Is the given string a series of digits.
      Parameters:
      str - The string
      Returns:
      True if it is a series of digits
    • parseLocale

      @Nullable public static @Nullable Locale parseLocale(String localeValue)
      Parse the given String value into a Locale, accepting the Locale.toString() format as well as BCP 47 language tags.
      Parameters:
      localeValue - the locale value: following either Locale's toString() format ("en", "en_UK", etc), also accepting spaces as separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK") as specified by Locale.forLanguageTag(java.lang.String) on Java 7+

      Copied from the Spring Framework while retaining all license, copyright and author information.

      Returns:
      a corresponding Locale instance, or null if none
      Throws:
      IllegalArgumentException - in case of an invalid locale specification
      Since:
      2.3.0
      See Also:
    • trimLeadingWhitespace

      public static String trimLeadingWhitespace(String str)
      Returns a new string without any leading whitespace.
      Parameters:
      str - The string
      Returns:
      The string without leading whitespace
      Since:
      2.3.0
    • trimLeadingCharacter

      public static String trimLeadingCharacter(String str, char c)
      Returns a new string without any leading characters that match the supplied character.
      Parameters:
      str - The string
      c - The character to remove
      Returns:
      The string without leading characters matching the supplied character.
      Since:
      2.3.0
    • trimLeading

      public static String trimLeading(String str, Predicate<Character> predicate)
      Returns a new string without any leading characters that match the supplied predicate.
      Parameters:
      str - The string
      predicate - The predicate to test characters against
      Returns:
      The string without leading characters matching the supplied predicate.
      Since:
      2.3.0
    • tokenizeToStringArray

      public static String[] tokenizeToStringArray(String str, 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

      Copied from the Spring Framework while retaining all license, copyright and author information.

      Parameters:
      str - 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:
    • 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 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

      Copied from the Spring Framework while retaining all license, copyright and author information.

      Parameters:
      str - the String to tokenize
      delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
      trimTokens - trim the tokens via String's 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).
      Returns:
      an array of the tokens (null if the input String was null)
      See Also:
    • convertDotToUnderscore

      public static String convertDotToUnderscore(String dottedProperty)
      Replace the dots in the property with underscore and transform to uppercase.
      Parameters:
      dottedProperty - The property with dots, example - a.b.c
      Returns:
      The converted value
    • convertDotToUnderscore

      public static String convertDotToUnderscore(String dottedProperty, boolean uppercase)
      Replace the dots in the property with underscore and transform to uppercase based on given flag.
      Parameters:
      dottedProperty - The property with dots, example - a.b.c
      uppercase - To transform to uppercase string
      Returns:
      The converted value
    • prependUri

      public static String prependUri(String baseUri, String uri)
      Prepends a partial uri and normalizes / characters. For example, if the base uri is "/foo/" and the uri is "/bar/", the output will be "/foo/bar/". Similarly if the base uri is "/foo" and the uri is "bar", the output will be "/foo/bar"
      Parameters:
      baseUri - The uri to prepend. Eg. /foo
      uri - The uri to combine with the baseUri. Eg. /bar
      Returns:
      A combined uri string
    • capitalize

      public static String capitalize(String str)
      Capitalizes the first character of the provided string.
      Parameters:
      str - The string to capitalize
      Returns:
      The capitalized string
    • trimToNull

      @Nullable public static @Nullable String trimToNull(@Nullable @Nullable String string)
      Trims the supplied string. If the string is empty or null before or after trimming, null is returned.
      Parameters:
      string - the string to trim
      Returns:
      the trimmed string or null
    • isTrue

      public static boolean isTrue(String booleanString)
      Is the boolean string true. Values that represent true are: yes, y, on, and true.
      Parameters:
      booleanString - The boolean string
      Returns:
      True if it is a valid value
    • splitOmitEmptyStrings

      public static Iterable<String> splitOmitEmptyStrings(CharSequence sequence, char splitCharacter)
      Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
      Parameters:
      sequence - The sequence to split
      splitCharacter - The split character
      Returns:
      The iterable of possible sequences
      Since:
      2.5.0
    • splitOmitEmptyStringsList

      public static List<String> splitOmitEmptyStringsList(CharSequence sequence, char splitCharacter)
      Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
      Parameters:
      sequence - The sequence to split
      splitCharacter - The split character
      Returns:
      The list of possible sequences
      Since:
      2.5.0
    • splitOmitEmptyStringsIterator

      public static Iterator<String> splitOmitEmptyStringsIterator(CharSequence sequence, char splitCharacter)
      Fast split by one character iterator. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
      Parameters:
      sequence - The sequence to split
      splitCharacter - The split character
      Returns:
      The iterator of possible sequences
      Since:
      2.5.0