Class Strings


  • public final class Strings
    extends Object
    Since:
    2.0
    • Method Detail

      • nullToEmpty

        public static String nullToEmpty​(@Nullable
                                         String string)
        Returns the given string if it is non-null; the empty string otherwise.
        Parameters:
        string - the string to test and possibly return
        Returns:
        string itself if it is non-null; "" if it is null
      • emptyToNull

        @Nullable
        public static String emptyToNull​(@Nullable
                                         String string)
        Returns the given string if it is nonempty; null otherwise.
        Parameters:
        string - the string to test and possibly return
        Returns:
        string itself if it is nonempty; null if it is empty or null
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(@Nullable
                                            String string)
        Returns true if the given string is null or is the empty string.

        Consider normalizing your string references with nullToEmpty(java.lang.String). If you do, you can use String.isEmpty() instead of this method, and you won't need special null-safe forms of methods like String.toUpperCase(java.util.Locale) either. Or, if you'd like to normalize "in the other direction," converting empty strings to null, you can use emptyToNull(java.lang.String).

        Parameters:
        string - a string reference to check
        Returns:
        true if the string is null or is the empty string
      • repeat

        public static String repeat​(String string,
                                    int count)
        Returns a string consisting of a specific number of concatenated copies of an input string. For example, repeat("hey", 3) returns the string "heyheyhey".
        Parameters:
        string - any non-null string
        count - the number of times to repeat it; a nonnegative integer
        Returns:
        a string containing string repeated count times (the empty string if count is zero)
        Throws:
        IllegalArgumentException - if count is negative