Class CharSequenceUtils

  • All Implemented Interfaces:
    Utils

    public abstract class CharSequenceUtils
    extends java.lang.Object
    implements Utils
    The utilities class for CharSequence
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    CharSequence
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsWhitespace​(java.lang.CharSequence str)
      Checks whether the given CharSequence contains any whitespace characters.
      static boolean isEmpty​(java.lang.CharSequence value)
      Checks if the provided CharSequence is empty.
      static boolean isNotEmpty​(java.lang.CharSequence value)
      Checks if the provided CharSequence is not empty.
      static int length​(java.lang.CharSequence value)
      Returns the length of the provided CharSequence.
      static java.lang.CharSequence trimAllWhitespace​(java.lang.CharSequence str)
      Trims all whitespace characters from the given CharSequence.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • length

        public static int length​(@Nullable
                                 java.lang.CharSequence value)
        Returns the length of the provided CharSequence.

        If the provided value is null, this method returns 0. Otherwise, it returns the result of calling the CharSequence.length() method.

        Example Usage

        • length(null) returns 0
        • length("") returns 0
        • length("hello")} returns 5
        Parameters:
        value - the CharSequence to get the length from
        Returns:
        the length of the provided value, or 0 if it is null
      • isEmpty

        public static boolean isEmpty​(@Nullable
                                      java.lang.CharSequence value)
        Checks if the provided CharSequence is empty.

        A CharSequence is considered empty if its length is 0. This method returns true if the provided value is null or has a length of 0.

        Example Usage

        • isEmpty(null) returns true
        • isEmpty("") returns true
        • isEmpty("hello")} returns false
        Parameters:
        value - the CharSequence to check
        Returns:
        true if the provided CharSequence is empty or null, otherwise false
      • isNotEmpty

        public static boolean isNotEmpty​(@Nullable
                                         java.lang.CharSequence value)
        Checks if the provided CharSequence is not empty.

        A CharSequence is considered not empty if its length is greater than 0. This method returns true if the provided value is not null and has a length greater than 0.

        Example Usage

        • isNotEmpty(null) returns false
        • isNotEmpty("") returns false
        • isNotEmpty("hello")} returns true
        Parameters:
        value - the CharSequence to check
        Returns:
        true if the provided CharSequence is not empty, otherwise false
      • containsWhitespace

        public static boolean containsWhitespace​(@Nullable
                                                 java.lang.CharSequence str)
        Checks whether the given CharSequence contains any whitespace characters.

        A whitespace character is defined as any character that returns true when passed to Character.isWhitespace(char).

        Example Usage

        • containsWhitespace(null) returns false
        • containsWhitespace("") returns false
        • containsWhitespace("hello world") returns true
        • containsWhitespace("hello\tworld") returns true
        • containsWhitespace("helloworld") returns false
        Parameters:
        str - the CharSequence to check (may be null)
        Returns:
        true if the provided sequence is not empty and contains at least one whitespace character; otherwise, false
      • trimAllWhitespace

        public static java.lang.CharSequence trimAllWhitespace​(java.lang.CharSequence str)
        Trims all whitespace characters from the given CharSequence.

        This method removes all whitespace characters (as defined by Character.isWhitespace(char)) from the beginning, end, and middle of the input sequence. If the input is null or empty, it will be returned as-is.

        Example Usage

        • trimAllWhitespace(null) returns null
        • trimAllWhitespace("") returns ""
        • trimAllWhitespace(" hello world ") returns "helloworld"
        • trimAllWhitespace(" \t\n h e l l o \r\n\f") returns "hello"
        Parameters:
        str - the CharSequence to trim (may be null)
        Returns:
        a new CharSequence with all whitespace characters removed, or the original if none exist