Class CharSequences


  • public final class CharSequences
    extends Object
    Utility methods for dealing with CharSequence objects.
    • Method Detail

      • isNumeric

        public static boolean isNumeric​(CharSequence charSequence)
        Checks if the given char sequences is numeric.

        A char sequence is considered to be numeric if it contains entirely out of numbers between 0 and 9. Empty sequences are rejected as are ones with a leading -.

        Parameters:
        charSequence - the sequence to check
        Returns:
        if the sequence is entirely made of of number
        Throws:
        NullPointerException - if the char sequence is null
      • parseInt

        public static int parseInt​(CharSequence charSequence,
                                   int beginIndex,
                                   int endIndex)
        Parses a given char sequence compatible to Integer.parseInt(String).
        Parameters:
        charSequence - the CharSequence containing the int representation to be parsed, null will cause a NumberFormatException to be thrown
        beginIndex - the inclusive index at which to star
        endIndex - the exclusive index at which to end
        Returns:
        the integer value represented by the argument in decimal
        Throws:
        NumberFormatException - if the charSequence does not contain a parsable int
        IndexOutOfBoundsException - if beginIndex is less than endIndex, if beginIndex is negative or if endIndex is bigger than the length of charSequence
        See Also:
        Integer.parseInt(String), isNumeric(CharSequence)
        Implementation Note:
        no allocation is performed, performance can be 35% to 50% better than Integer#parseInt(String)
      • parseInt

        public static int parseInt​(CharSequence charSequence)
        Parses a given char sequence compatible to Integer.parseInt(String).
        Parameters:
        charSequence - the CharSequence containing the int representation to be parsed, null will cause a NumberFormatException to be thrown
        Returns:
        the integer value represented by the argument in decimal
        Throws:
        NumberFormatException - if the charSequence does not contain a parsable int
        See Also:
        Integer.parseInt(String), isNumeric(CharSequence)
        Implementation Note:
        no allocation is performed, performance can be 35% to 50% better than Integer#parseInt(String)
      • parseLong

        public static long parseLong​(CharSequence charSequence)
        Parses a given char sequence compatible to Long.parseLong(String).
        Parameters:
        charSequence - the CharSequence containing the long representation to be parsed, null will cause a NumberFormatException to be thrown
        Returns:
        the long value represented by the argument in decimal
        Throws:
        NumberFormatException - if the charSequence does not contain a parsable long
        See Also:
        Long.parseLong(java.lang.String, int), isNumeric(CharSequence)
        Implementation Note:
        no allocation is performed, performance can be 20% to 50% better than Long#parseLong(String)
      • parseLong

        public static long parseLong​(CharSequence charSequence,
                                     int beginIndex,
                                     int endIndex)
        Parses a given char sequence compatible to Long.parseLong(String).
        Parameters:
        charSequence - the CharSequence containing the long representation to be parsed, null will cause a NumberFormatException to be thrown
        beginIndex - the inclusive index at which to star
        endIndex - the exclusive index at which to end
        Returns:
        the long value represented by the argument in decimal
        Throws:
        NumberFormatException - if the charSequence does not contain a parsable long
        IndexOutOfBoundsException - if beginIndex is less than endIndex, if beginIndex is negative or if endIndex is bigger than the length of charSequence
        See Also:
        Long.parseLong(java.lang.String, int), isNumeric(CharSequence)
        Implementation Note:
        no allocation is performed, performance can be 20% to 50% better than Long#parseLong(String)
      • indexOf

        public static int indexOf​(CharSequence charSequence,
                                  char c)
        Searches for the first occurrence of a char within a sequence that's compatible with String.indexOf(int).
        Parameters:
        charSequence - the sequence within to search, not null
        c - the char to search for
        Returns:
        the index of the first occurrence of the specified char, or -1 if there is no such occurrence
        See Also:
        String.indexOf(int)
      • indexOf

        public static int indexOf​(CharSequence charSequence,
                                  char c,
                                  int fromIndex)
        Searches for the first occurrence of a char within a sequence staring at a given index that's compatible with String.indexOf(int, int).
        Parameters:
        charSequence - the sequence within to search, not null
        c - the char to search for
        fromIndex - the index at which to start the search from, if it's larger than the length of this string then -1 is returned if it's negative then it is treated as 0
        Returns:
        the index of the first occurrence of the specified char after fromIndex, or -1 if there is no such occurrence
        See Also:
        String.indexOf(int, int)
      • lastIndexOf

        public static int lastIndexOf​(CharSequence charSequence,
                                      char c)
        Searches for the last occurrence of a char within a sequence that's compatible with String.indexOf(int).
        Parameters:
        charSequence - the sequence within to search, not null
        c - the char to search for
        Returns:
        the index of the last occurrence of the specified char, or -1 if there is no such occurrence
        See Also:
        String.indexOf(int)
      • indexOf

        public static int indexOf​(CharSequence charSequence,
                                  String subSequence)
        Searches for the first occurrence subsequence within a sequence that's compatible with String.indexOf(String):
        Parameters:
        charSequence - the sequence within to search, not null
        subSequence - the subsequence to search for, not null
        Returns:
        the index of the first char of the first occurrence of the specified subsequence, or -1 if there is no such occurrence
        See Also:
        String.indexOf(String)
      • trim

        public static CharSequence trim​(CharSequence charSequence)
        Returns a sequence whose value is the given sequence, with any leading and trailing spaces removed.
        Parameters:
        charSequence - the sequence to trim, not null
        Returns:
        the trimmed sequence
        Throws:
        NullPointerException - if the given sequence is null
        See Also:
        String.trim(), CharSequence.subSequence(int, int)
        Implementation Note:
        allocation avoided if the sequence does not start or end with a space, allocation avoided if the sequence is just spaces
      • split

        public static Iterable<CharSequence> split​(CharSequence charSequence,
                                                   char delimiter)
        Splits the given CharSequence around matches of the given delimiter character.
        Parameters:
        charSequence - the CharSequence to split
        delimiter - the delimiting character
        Returns:
        iterable of CharSequence computed by splitting the given CharSequence around matches of the given delimiter character
        Implementation Note:
        the iterable is lazily computed, no backing collection is created, calling Iterable.iterator() multiple times will create a new iterator starting from the first occurrence every time
      • startsWithBom

        public static boolean startsWithBom​(CharSequence charSequence)
        Checks if a sequence starts with a byte order mark.
        Parameters:
        charSequence - the CharSequence to check, not null
        Returns:
        if a sequence starts with a BOM
      • removeLeadingBom

        public static CharSequence removeLeadingBom​(CharSequence charSequence)
        Removes a leading byte order mark from a sequence if present.
        Parameters:
        charSequence - the CharSequence from which to remove the BOM, not null
        Returns:
        a subsequence with the leading BOM removed the same sequence if the first character is not a BOM or the sequence is empty
      • startsWith

        public static boolean startsWith​(CharSequence charSequence,
                                         CharSequence prefix)
        Tests if the given CharSequence starts with the specified prefix.
        Parameters:
        charSequence - the CharSequence to test
        prefix - the prefix
        Returns:
        true if the character sequence represented by the prefix argument is a prefix of the character sequence represented by the charSequence argument, false otherwise. Note also that true will be returned if the argument is an empty string or is equal to the given CharSequence on a char by char basis
        Throws:
        NullPointerException - if any the given sequences is null
        See Also:
        String.startsWith(String)