Class CharSequences
- java.lang.Object
-
- com.github.marschall.charsequences.CharSequences
-
public final class CharSequences extends Object
Utility methods for dealing withCharSequence
objects.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
indexOf(CharSequence charSequence, char c)
Searches for the first occurrence of a char within a sequence that's compatible withString.indexOf(int)
.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 withString.indexOf(int, int)
.static int
indexOf(CharSequence charSequence, String subSequence)
Searches for the first occurrence subsequence within a sequence that's compatible withString.indexOf(String)
:static boolean
isNumeric(CharSequence charSequence)
Checks if the given char sequences is numeric.static int
lastIndexOf(CharSequence charSequence, char c)
Searches for the last occurrence of a char within a sequence that's compatible withString.indexOf(int)
.static int
parseInt(CharSequence charSequence)
Parses a given char sequence compatible toInteger.parseInt(String)
.static int
parseInt(CharSequence charSequence, int beginIndex, int endIndex)
Parses a given char sequence compatible toInteger.parseInt(String)
.static long
parseLong(CharSequence charSequence)
Parses a given char sequence compatible toLong.parseLong(String)
.static long
parseLong(CharSequence charSequence, int beginIndex, int endIndex)
Parses a given char sequence compatible toLong.parseLong(String)
.static CharSequence
removeLeadingBom(CharSequence charSequence)
Removes a leading byte order mark from a sequence if present.static Iterable<CharSequence>
split(CharSequence charSequence, char delimiter)
Splits the given CharSequence around matches of the given delimiter character.static boolean
startsWith(CharSequence charSequence, CharSequence prefix)
Tests if the given CharSequence starts with the specified prefix.static boolean
startsWithBom(CharSequence charSequence)
Checks if a sequence starts with a byte order mark.static CharSequence
trim(CharSequence charSequence)
Returns a sequence whose value is the given sequence, with any leading and trailing spaces removed.static UUID
uuidFromCharSequence(CharSequence name)
Creates a UUID from aCharSequence
likeUUID.fromString(String)
-
-
-
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
and9
. 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 isnull
-
parseInt
public static int parseInt(CharSequence charSequence, int beginIndex, int endIndex)
Parses a given char sequence compatible toInteger.parseInt(String)
.- Parameters:
charSequence
- theCharSequence
containing theint
representation to be parsed,null
will cause aNumberFormatException
to be thrownbeginIndex
- the inclusive index at which to starendIndex
- 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 intIndexOutOfBoundsException
- 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 toInteger.parseInt(String)
.- Parameters:
charSequence
- theCharSequence
containing theint
representation to be parsed,null
will cause aNumberFormatException
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 toLong.parseLong(String)
.- Parameters:
charSequence
- theCharSequence
containing thelong
representation to be parsed,null
will cause aNumberFormatException
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 toLong.parseLong(String)
.- Parameters:
charSequence
- theCharSequence
containing thelong
representation to be parsed,null
will cause aNumberFormatException
to be thrownbeginIndex
- the inclusive index at which to starendIndex
- 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 longIndexOutOfBoundsException
- 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 withString.indexOf(int)
.- Parameters:
charSequence
- the sequence within to search, notnull
c
- thechar
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 withString.indexOf(int, int)
.- Parameters:
charSequence
- the sequence within to search, notnull
c
- thechar
to search forfromIndex
- 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 as0
- 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 withString.indexOf(int)
.- Parameters:
charSequence
- the sequence within to search, notnull
c
- thechar
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 withString.indexOf(String)
:- Parameters:
charSequence
- the sequence within to search, notnull
subSequence
- the subsequence to search for, notnull
- 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, notnull
- 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 splitdelimiter
- 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, notnull
- 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, notnull
- 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 testprefix
- the prefix- Returns:
true
if the character sequence represented by theprefix
argument is a prefix of the character sequence represented by thecharSequence
argument,false
otherwise. Note also thattrue
will be returned if the argument is an empty string or is equal to the givenCharSequence
on achar
bychar
basis- Throws:
NullPointerException
- if any the given sequences isnull
- See Also:
String.startsWith(String)
-
uuidFromCharSequence
public static UUID uuidFromCharSequence(CharSequence name)
Creates a UUID from aCharSequence
likeUUID.fromString(String)
- Parameters:
name
- the char sequence that specifies aUUID
, notnull
- Returns:
- a new
UUID
with the specified value - Throws:
IllegalArgumentException
- ifname
is not in the format specifiedNullPointerException
- ifname
isnull
- See Also:
UUID.fromString(String)
,UUID.toString()
- Implementation Note:
- unlike
UUID.fromString(String)
performs no allocation besides theUUID
, on a 64bit JVM with oop compression a UUID is 32 and the size of a UUID string is 24 bytes for the string plus 88 for the backing character array for a total of 112 bytes (on Java 8 at least)
-
-