public final class CharSequences extends Static
CharSequence
instances. Some methods defined in this
class duplicate the functionalities already provided in the standard String
class,
but works on a generic CharSequence
instance instead than String
.
Character.isWhitespace(int)
and Character.isSpaceChar(int)
.
Those two methods differ in the way they handle no-break spaces, tabulations and line feeds. The general policy in the SIS library is:
isWhitespace(…)
when separating entities (words, numbers, tokens, etc.)
in a list. Using that method, characters separated by a no-break space are considered as
part of the same entity.isSpaceChar(…)
when parsing a single entity, for example a single word.
Using this method, no-break spaces are considered as part of the entity while line
feeds or tabulations are entity boundaries.isWhitespace(…)
is appropriate for skipping spaces between the numbers.
But if there is spaces to skip inside a single number, then isSpaceChar(…)
is a good choice
for accepting no-break spaces and for stopping the parse operation at tabulations or line feed character.
A tabulation or line feed between two characters is very likely to separate two distinct values.Format
implementations in the SIS library typically use
isSpaceChar(…)
while most of the rest of the SIS library, including this
CharSequences
class, consistently uses isWhitespace(…)
.
Note that the String.trim()
method doesn't follow any of those policies and should
generally be avoided. That trim()
method removes every ISO control characters without
distinction about whether the characters are space or not, and ignore all Unicode spaces.
The trimWhitespaces(String)
method defined in this class can be used as an alternative.
null
CharSequence
argument. In such cases
the method return value is either a null
CharSequence
, an empty array, or a
0
or false
primitive type calculated as if the input was an empty string.StringBuilders
Defined in the sis-utility
module
Modifier and Type | Field and Description |
---|---|
static String[] |
EMPTY_ARRAY
An array of zero-length.
|
Modifier and Type | Method and Description |
---|---|
static CharSequence |
camelCaseToAcronym(CharSequence text)
Creates an acronym from the given text.
|
static CharSequence |
camelCaseToSentence(CharSequence identifier)
Given a string in camel cases (typically an identifier), returns a string formatted
like an English sentence.
|
static CharSequence |
camelCaseToWords(CharSequence identifier,
boolean toLowerCase)
Given a string in camel cases, returns a string with the same words separated by spaces.
|
static int |
codePointCount(CharSequence text)
Returns the number of Unicode code points in the given characters sequence,
or 0 if
null . |
static int |
codePointCount(CharSequence text,
int fromIndex,
int toIndex)
Returns the number of Unicode code points in the given characters sub-sequence,
or 0 if
null . |
static CharSequence |
commonPrefix(CharSequence s1,
CharSequence s2)
Returns the longest sequence of characters which is found at the beginning of the two
given texts.
|
static CharSequence |
commonSuffix(CharSequence s1,
CharSequence s2)
Returns the longest sequence of characters which is found at the end of the two given texts.
|
static void |
copyChars(CharSequence src,
int srcOffset,
char[] dst,
int dstOffset,
int length)
Copies a sequence of characters in the given
char[] array. |
static int |
count(CharSequence text,
char toSearch)
Counts the number of occurrence of the given character in the given character sequence.
|
static int |
count(CharSequence text,
String toSearch)
Returns the number of occurrences of the
toSearch string in the given text . |
static boolean |
endsWith(CharSequence text,
CharSequence suffix,
boolean ignoreCase)
Returns
true if the given character sequence ends with the given suffix. |
static boolean |
equals(CharSequence s1,
CharSequence s2)
Returns
true if the two given texts are equal. |
static boolean |
equalsFiltered(CharSequence s1,
CharSequence s2,
Characters.Filter filter,
boolean ignoreCase)
Returns
true if the given texts are equal, optionally ignoring case and filtered-out characters. |
static boolean |
equalsIgnoreCase(CharSequence s1,
CharSequence s2)
Returns
true if the two given texts are equal, ignoring case. |
static int |
indexOf(CharSequence text,
CharSequence toSearch,
int fromIndex,
int toIndex)
Returns the index within the given strings of the first occurrence of the specified part,
starting at the specified index.
|
static int |
indexOf(CharSequence text,
int toSearch,
int fromIndex,
int toIndex)
Returns the index within the given character sequence of the first occurrence of the
specified character, starting the search at the specified index.
|
static int |
indexOfLineStart(CharSequence text,
int numLines,
int fromIndex)
Returns the index of the first character after the given number of lines.
|
static boolean |
isAcronymForWords(CharSequence acronym,
CharSequence words)
Returns
true if the first string is likely to be an acronym of the second string. |
static boolean |
isUnicodeIdentifier(CharSequence identifier)
Returns
true if the given identifier is a legal Unicode identifier. |
static boolean |
isUpperCase(CharSequence text)
Returns
true if the given text is non-null, contains at least one upper-case character and
no lower-case character. |
static int |
lastIndexOf(CharSequence text,
int toSearch,
int fromIndex,
int toIndex)
Returns the index within the given character sequence of the last occurrence of the
specified character, searching backward in the given index range.
|
static int |
length(CharSequence text)
Returns the length of the given characters sequence,
or 0 if
null . |
static byte[] |
parseBytes(CharSequence values,
char separator,
int radix)
|
static double[] |
parseDoubles(CharSequence values,
char separator)
|
static float[] |
parseFloats(CharSequence values,
char separator)
|
static int[] |
parseInts(CharSequence values,
char separator,
int radix)
|
static long[] |
parseLongs(CharSequence values,
char separator,
int radix)
|
static short[] |
parseShorts(CharSequence values,
char separator,
int radix)
|
static boolean |
regionMatches(CharSequence text,
int fromIndex,
CharSequence part)
Returns
true if the given text at the given offset contains the given part,
in a case-sensitive comparison. |
static boolean |
regionMatches(CharSequence text,
int fromIndex,
CharSequence part,
boolean ignoreCase)
Returns
true if the given text at the given offset contains the given part,
optionally in a case-insensitive way. |
static CharSequence |
replace(CharSequence text,
CharSequence toSearch,
CharSequence replaceBy)
Replaces all occurrences of a given string in the given character sequence.
|
static CharSequence |
shortSentence(CharSequence text,
int maxLength)
Makes sure that the
text string is not longer than maxLength characters. |
static int |
skipLeadingWhitespaces(CharSequence text,
int fromIndex,
int toIndex)
Returns the index of the first non-white character in the given range.
|
static int |
skipTrailingWhitespaces(CharSequence text,
int fromIndex,
int toIndex)
Returns the index after the last non-white character in the given range.
|
static CharSequence |
spaces(int length)
Returns a character sequence of the specified length filled with white spaces.
|
static CharSequence[] |
split(CharSequence text,
char separator)
Splits a text around the given character.
|
static CharSequence[] |
splitOnEOL(CharSequence text)
Splits a text around the End Of Line (EOL) characters.
|
static boolean |
startsWith(CharSequence text,
CharSequence prefix,
boolean ignoreCase)
Returns
true if the given character sequence starts with the given prefix. |
static CharSequence |
toASCII(CharSequence text)
Replaces some Unicode characters by ASCII characters on a "best effort basis".
|
static CharSequence |
token(CharSequence text,
int fromIndex)
Returns the token starting at the given offset in the given text.
|
static CharSequence |
trimFractionalPart(CharSequence value)
Trims the fractional part of the given formatted number, provided that it doesn't change
the value.
|
static CharSequence |
trimWhitespaces(CharSequence text)
Returns a text with leading and trailing whitespace characters omitted.
|
static CharSequence |
trimWhitespaces(CharSequence text,
int lower,
int upper)
Returns a sub-sequence with leading and trailing whitespace characters omitted.
|
static String |
trimWhitespaces(String text)
Returns a string with leading and trailing whitespace characters omitted.
|
static CharSequence |
upperCaseToSentence(CharSequence identifier)
Given a string in upper cases (typically a Java constant), returns a string formatted
like an English sentence.
|
public static final String[] EMPTY_ARRAY
Collections.EMPTY_LIST
.public static CharSequence spaces(int length)
length
argument by (desired width - used width).
Since the used width value may be greater than expected, this method handle negative length
values as if the value was zero.length
- the string length. Negative values are clamped to 0.length
filled with white spaces.public static int length(CharSequence text)
null
.text
- the character sequence from which to get the length, or null
.null
.public static int codePointCount(CharSequence text)
null
. Unpaired surrogates within the text count as one code
point each.text
- the character sequence from which to get the count, or null
.null
.codePointCount(CharSequence, int, int)
public static int codePointCount(CharSequence text, int fromIndex, int toIndex)
null
. Unpaired surrogates within the text count as one code
point each.
This method performs the same work than the standard
Character.codePointCount(CharSequence, int, int)
method, except that it tries
to delegate to the optimized methods from the String
, StringBuilder
,
StringBuffer
or CharBuffer
classes if possible.
text
- the character sequence from which to get the count, or null
.fromIndex
- the index from which to start the computation.toIndex
- the index after the last character to take in account.null
.Character.codePointCount(CharSequence, int, int)
,
String.codePointCount(int, int)
,
StringBuilder.codePointCount(int, int)
public static int count(CharSequence text, String toSearch)
toSearch
string in the given text
.
The search is case-sensitive.text
- the character sequence to count occurrences, or null
.toSearch
- the string to search in the given text
.
It shall contain at least one character.toSearch
in text
,
or 0 if text
was null or empty.NullArgumentException
- if the toSearch
argument is null.IllegalArgumentException
- if the toSearch
argument is empty.public static int count(CharSequence text, char toSearch)
text
- the character sequence to count occurrences, or null
.toSearch
- the character to count.text
is null.public static int indexOf(CharSequence text, CharSequence toSearch, int fromIndex, int toIndex)
CharSequence
objects instead than
String
s only, and that the upper limit can be specified:
return text.indexOf(part, fromIndex);There is no restriction on the value of
fromIndex
. If negative or greater
than toIndex
, then the behavior of this method is as if the search started
from 0 or toIndex
respectively. This is consistent with the
String.indexOf(String, int)
behavior.text
- the string in which to perform the search.toSearch
- the substring for which to search.fromIndex
- the index from which to start the search.toIndex
- the index after the last character where to perform the search.text
argument is null.NullArgumentException
- if the toSearch
argument is null.IllegalArgumentException
- if the toSearch
argument is empty.String.indexOf(String, int)
,
StringBuilder.indexOf(String, int)
,
StringBuffer.indexOf(String, int)
public static int indexOf(CharSequence text, int toSearch, int fromIndex, int toIndex)
There is no restriction on the value of fromIndex
. If negative or greater
than toIndex
, then the behavior of this method is as if the search started
from 0 or toIndex
respectively. This is consistent with the behavior documented
in String.indexOf(int, int)
.
text
- the character sequence in which to perform the search, or null
.toSearch
- the Unicode code point of the character to search.fromIndex
- the index to start the search from.toIndex
- the index after the last character where to perform the search.text
argument is null.String.indexOf(int, int)
public static int lastIndexOf(CharSequence text, int toSearch, int fromIndex, int toIndex)
There is no restriction on the value of toIndex
. If greater than the text length
or less than fromIndex
, then the behavior of this method is as if the search started
from length
or fromIndex
respectively. This is consistent with the behavior
documented in String.lastIndexOf(int, int)
.
text
- the character sequence in which to perform the search, or null
.toSearch
- the Unicode code point of the character to search.fromIndex
- the index of the first character in the range where to perform the search.toIndex
- the index after the last character in the range where to perform the search.text
argument is null.String.lastIndexOf(int, int)
public static int indexOfLineStart(CharSequence text, int numLines, int fromIndex)
'\n'
, '\r'
or "\r\n"
starting from the given position. When numLines
occurrences have been found, the index of the first character after the last
occurrence is returned.
If the numLines
argument is positive, this method searches forward.
If negative, this method searches backward. If 0, this method returns the
beginning of the current line.
If this method reaches the end of text
while searching forward, then
text.length()
is returned. If this method reaches the beginning of
text
while searching backward, then 0 is returned.
text
- the string in which to skip a determined amount of lines.numLines
- the number of lines to skip. Can be positive, zero or negative.fromIndex
- index at which to start the search, from 0 to text.length()
inclusive.NullPointerException
- if the text
argument is null.IndexOutOfBoundsException
- if fromIndex
is out of bounds.public static int skipLeadingWhitespaces(CharSequence text, int fromIndex, int toIndex)
toIndex
.
Note that this character may not exist if toIndex
is equals to the text length.
Special cases:
fromIndex
is greater than toIndex
,
then this method unconditionally returns fromIndex
.toIndex-1
is the high surrogate of a valid supplementary code point, then this method returns
toIndex+1
, which is the index of the next code point.fromIndex
is negative or toIndex
is greater than the text length,
then the behavior of this method is undefined.Character.isWhitespace(int)
method.text
- the string in which to perform the search (can not be null).fromIndex
- the index from which to start the search (can not be negative).toIndex
- the index after the last character where to perform the search.toIndex
if none.NullPointerException
- if the text
argument is null.skipTrailingWhitespaces(CharSequence, int, int)
,
trimWhitespaces(CharSequence)
public static int skipTrailingWhitespaces(CharSequence text, int fromIndex, int toIndex)
fromIndex
.
Special cases:
fromIndex
is lower than toIndex
,
then this method unconditionally returns toIndex
.fromIndex
is the low surrogate of a valid supplementary code point, then this method returns
fromIndex-1
, which is the index of the code point.fromIndex
is negative or toIndex
is greater than the text length,
then the behavior of this method is undefined.Character.isWhitespace(int)
method.text
- the string in which to perform the search (can not be null).fromIndex
- the index from which to start the search (can not be negative).toIndex
- the index after the last character where to perform the search.fromIndex
if none.NullPointerException
- if the text
argument is null.skipLeadingWhitespaces(CharSequence, int, int)
,
trimWhitespaces(CharSequence)
public static CharSequence[] split(CharSequence text, char separator)
This method is similar to the standard String.split(String)
method except for the
following:
null
argument, in which case an empty array is returned.separator
argument is '\n'
or '\r'
, then this method
splits around any of "\r"
, "\n"
or "\r\n"
characters sequences.
text
- the text to split, or null
.separator
- the delimiting character (typically the coma).toSplit
was null.String.split(String)
public static CharSequence[] splitOnEOL(CharSequence text)
"\r"
, "\n"
or "\r\n"
sequences.
Each element in the returned array will be a single line. If the given text is already
a single line, then this method returns a singleton containing only the given text.
Notes:
split(toSplit, '\n’)
,
this method does not remove whitespaces.String.substring(int,int)
shared the same char[]
internal array.
However since JDK8, the new String
implementation copies the data in new arrays.
Consequently it is better to use index rather than this method for splitting large String
s.
However this method still useful for other CharSequence
implementations providing an efficient
subSequence(int,int)
method.text
- the multi-line text from which to get the individual lines, or null
.indexOfLineStart(CharSequence, int, int)
public static double[] parseDoubles(CharSequence values, char separator) throws NumberFormatException
double
.
Empty sub-sequences are parsed as Double.NaN
.values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).values
was null.NumberFormatException
- if at least one number can not be parsed.public static float[] parseFloats(CharSequence values, char separator) throws NumberFormatException
float
.
Empty sub-sequences are parsed as Float.NaN
.values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).values
was null.NumberFormatException
- if at least one number can not be parsed.public static long[] parseLongs(CharSequence values, char separator, int radix) throws NumberFormatException
values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).radix
- the radix to be used for parsing. This is usually 10.values
was null.NumberFormatException
- if at least one number can not be parsed.public static int[] parseInts(CharSequence values, char separator, int radix) throws NumberFormatException
values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).radix
- the radix to be used for parsing. This is usually 10.values
was null.NumberFormatException
- if at least one number can not be parsed.public static short[] parseShorts(CharSequence values, char separator, int radix) throws NumberFormatException
values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).radix
- the radix to be used for parsing. This is usually 10.values
was null.NumberFormatException
- if at least one number can not be parsed.public static byte[] parseBytes(CharSequence values, char separator, int radix) throws NumberFormatException
values
- the text containing the values to parse, or null
.separator
- the delimiting character (typically the coma).radix
- the radix to be used for parsing. This is usually 10.values
was null.NumberFormatException
- if at least one number can not be parsed.public static CharSequence toASCII(CharSequence text)
Transliterator
for more information.text
- the text to scan for Unicode characters to replace by ASCII characters, or null
.text
if no replacement
has been applied, or null
if the given text was null.StringBuilders.toASCII(StringBuilder)
,
Transliterator.filter(String)
,
Normalizer
public static String trimWhitespaces(String text)
String.trim()
, except that the later considers
every ISO control codes below 32 to be a whitespace.
That String.trim()
behavior has the side effect of removing the heading of ANSI escape
sequences (a.k.a. X3.64), and to ignore Unicode spaces. This trimWhitespaces(…)
method
is built on the more accurate Character.isWhitespace(int)
method instead.
This method performs the same work than trimWhitespaces(CharSequence)
,
but is overloaded for the String
type because of its frequent use.
text
- the text from which to remove leading and trailing whitespaces, or null
.null
is the given
text was null.public static CharSequence trimWhitespaces(CharSequence text)
Character.isWhitespace(int)
method.
This method is the generic version of trimWhitespaces(String)
.
text
- the text from which to remove leading and trailing whitespaces, or null
.null
is the given text was null.skipLeadingWhitespaces(CharSequence, int, int)
,
skipTrailingWhitespaces(CharSequence, int, int)
public static CharSequence trimWhitespaces(CharSequence text, int lower, int upper)
Character.isWhitespace(int)
method.
Invoking this method is functionally equivalent to the following code snippet,
except that the subSequence
method is
invoked only once instead of two times:
text = trimWhitespaces(text.subSequence(lower, upper));
text
- the text from which to remove leading and trailing white spaces.lower
- index of the first character to consider for inclusion in the sub-sequence.upper
- index after the last character to consider for inclusion in the sub-sequence.null
if the text
argument is null.IndexOutOfBoundsException
- if lower
or upper
is out of bounds.public static CharSequence trimFractionalPart(CharSequence value)
Double.toString(double)
method.
More specifically if the given value ends with a '.'
character followed by a
sequence of '0'
characters, then those characters are omitted. Otherwise this
method returns the text unchanged. This is a "all or nothing" method:
either the fractional part is completely removed, or either it is left unchanged.
"4"
if the given value is "4."
, "4.0"
or
"4.00"
, but returns "4.10"
unchanged (including the trailing '0'
character) if the input is "4.10"
.
value
- the value to trim if possible, or null
.".0"
part (if any),
or null
if the given text was null.StringBuilders.trimFractionalPart(StringBuilder)
public static CharSequence shortSentence(CharSequence text, int maxLength)
text
string is not longer than maxLength
characters.
If text
is not longer, then it is returned unchanged. Otherwise this method returns
a copy of text
with some characters substituted by the "(…)"
string.
If the text needs to be shortened, then this method tries to apply the above-cited substitution between two words. For example, the following text:
"This sentence given as an example is way too long to be included in a short name."May be shortened to something like this:
"This sentence given (…) in a short name."
text
- the sentence to reduce if it is too long, or null
.maxLength
- the maximum length allowed for text
.maxLength
, or null
if the given text was null.public static CharSequence upperCaseToSentence(CharSequence identifier)
'_'
by spaces.Character.toLowerCase(int)
. Note that this method does not use
the String.toLowerCase()
method. Consequently the system locale
is ignored. This method behaves as if the conversion were done in the
root locale.Note that those heuristic rules may be modified in future SIS versions, depending on the practical experience gained.
identifier
- the name of a Java constant, or null
.null
if the given identifier
argument was null.public static CharSequence camelCaseToSentence(CharSequence identifier)
camelCaseToWords(CharSequence, boolean)
, which separate the words
on the basis of character case. For example "transferFunctionType"
become
"transfer function type". This works fine for ISO 19115 identifiers.'_'
by spaces in order to take in account
an other common naming convention, which uses '_'
as a word separator. This
convention is used by netCDF attributes like "project_name"
.'_'
character,
then the identifier is returned "as is" except for the '_'
characters which are replaced by '-'
.
This work well for identifiers like "UTF-8"
or "ISO-LATIN-1"
for instance.
Note that those heuristic rules may be modified in future SIS versions, depending on the practical experience gained.
identifier
- an identifier with no space, words begin with an upper-case character, or null
.null
if the given identifier
argument was null.public static CharSequence camelCaseToWords(CharSequence identifier, boolean toLowerCase)
"PixelInterleavedSampleModel"
, then this method returns
"Pixel Interleaved Sample Model" or "Pixel interleaved sample model"
depending on the value of the toLowerCase
argument.
If toLowerCase
is false
, then this method inserts spaces but does not change
the case of characters. If toLowerCase
is true
, then this method changes
to lower case the first character after each spaces
inserted by this method (note that this intentionally exclude the very first character in
the given string), except if the second character is upper case, in which case the word is assumed an acronym.
The given string is usually a programmatic identifier like a class name or a method name.
identifier
- an identifier with no space, words begin with an upper-case character.toLowerCase
- true
for changing the first character of words to lower case,
except for the first word and acronyms.null
if the given identifier
argument was null.public static CharSequence camelCaseToAcronym(CharSequence text)
'_'
character, or any character which is
not a Unicode identifier part (including spaces).
An exception to the above rule happens if the given text is a Unicode identifier without the '_'
character, and every characters are upper case. In such case the text is returned unchanged on the assumption
that it is already an acronym.
Examples: given "northEast"
, this method returns "NE"
.
Given "Open Geospatial Consortium"
, this method returns "OGC"
.
text
- the text for which to create an acronym, or null
.null
if the given text was null.public static boolean isAcronymForWords(CharSequence acronym, CharSequence words)
true
if the first string is likely to be an acronym of the second string.
An acronym is a sequence of letters or digits
built from at least one character of each word in the words
string. More than
one character from the same word may appear in the acronym, but they must always
be the first consecutive characters. The comparison is case-insensitive.
"Open Geospatial Consortium"
words, the following strings are recognized as acronyms:
"OGC"
, "ogc"
, "O.G.C."
, "OpGeoCon"
.null
, this method returns false
.acronym
- a possible acronym of the sequence of words, or null
.words
- the sequence of words, or null
.true
if the first string is an acronym of the second one.public static boolean isUnicodeIdentifier(CharSequence identifier)
true
if the given identifier is a legal Unicode identifier.
This method returns true
if the identifier length is greater than zero,
the first character is a Unicode identifier start and all remaining characters (if any) are
Unicode identifier parts.
:
’, ‘-
’ and ‘.
’ characters,
which are legal in XML identifiers but not in Unicode.
Not legal in Unicode | Not legal in XML | ||
---|---|---|---|
: | (colon) | µ | (micro sign) |
- | (hyphen or minus) | ª | (feminine ordinal indicator) |
. | (dot) | º | (masculine ordinal indicator) |
· | (middle dot) | ⁔ | (inverted undertie) |
Many punctuation, symbols, etc. | Identifier ignorable characters. |
_
’ (underscore) character is legal according both Unicode and XML, while spaces,
‘!
’, ‘#
’, ‘*
’, ‘/
’, ‘?
’ and most other punctuation characters are not.
:
’, ‘-
’ and ‘.
’ which would normally be legal XML identifiers.
But since those characters could easily be confused with
namespace separators,
this exclusion is considered desirable.identifier
- the character sequence to test, or null
.true
if the given character sequence is a legal Unicode identifier.ImmutableIdentifier
,
Citations.getUnicodeIdentifier(Citation)
,
IdentifiedObjects.getUnicodeIdentifier(IdentifiedObject)
public static boolean isUpperCase(CharSequence text)
true
if the given text is non-null, contains at least one upper-case character and
no lower-case character. Space and punctuation are ignored.text
- the character sequence to test (may be null
).true
if non-null, contains at least one upper-case character and no lower-case character.String.toUpperCase()
public static boolean equalsFiltered(CharSequence s1, CharSequence s2, Characters.Filter filter, boolean ignoreCase)
true
if the given texts are equal, optionally ignoring case and filtered-out characters.
This method is sometime used for comparing identifiers in a lenient way.
Example: the following call compares the two strings ignoring case and any
characters which are not letter or digit.
In particular, spaces and punctuation characters like '_'
and '-'
are
ignored:
assert equalsFiltered("WGS84", "WGS_84", Characters.Filter.LETTERS_AND_DIGITS, true) == true;
s1
- the first characters sequence to compare, or null
.s2
- the second characters sequence to compare, or null
.filter
- the subset of characters to compare, or null
for comparing all characters.ignoreCase
- true
for ignoring cases, or false
for requiring exact match.true
if both arguments are null
or if the two given texts are equal,
optionally ignoring case and filtered-out characters.public static boolean equalsIgnoreCase(CharSequence s1, CharSequence s2)
true
if the two given texts are equal, ignoring case.
This method is similar to String.equalsIgnoreCase(String)
, except
it works on arbitrary character sequences and compares code points
instead than characters.s1
- the first string to compare, or null
.s2
- the second string to compare, or null
.true
if the two given texts are equal, ignoring case,
or if both arguments are null
.String.equalsIgnoreCase(String)
public static boolean equals(CharSequence s1, CharSequence s2)
true
if the two given texts are equal. This method delegates to
String.contentEquals(CharSequence)
if possible. This method never invoke
CharSequence.toString()
in order to avoid a potentially large copy of data.s1
- the first string to compare, or null
.s2
- the second string to compare, or null
.true
if the two given texts are equal, or if both arguments are null
.String.contentEquals(CharSequence)
public static boolean regionMatches(CharSequence text, int fromIndex, CharSequence part)
true
if the given text at the given offset contains the given part,
in a case-sensitive comparison. This method is equivalent to the following code,
except that this method works on arbitrary CharSequence
objects instead than
String
s only:
return text.regionMatches(offset, part, 0, part.length());This method does not thrown
IndexOutOfBoundsException
. Instead if
fromIndex < 0
or fromIndex + part.length() > text.length()
,
then this method returns false
.text
- the character sequence for which to tests for the presence of part
.fromIndex
- the offset in text
where to test for the presence of part
.part
- the part which may be present in text
.true
if text
contains part
at the given offset
.NullPointerException
- if any of the arguments is null.String.regionMatches(int, String, int, int)
public static boolean regionMatches(CharSequence text, int fromIndex, CharSequence part, boolean ignoreCase)
true
if the given text at the given offset contains the given part,
optionally in a case-insensitive way. This method is equivalent to the following code,
except that this method works on arbitrary CharSequence
objects instead than
String
s only:
return text.regionMatches(ignoreCase, offset, part, 0, part.length());This method does not thrown
IndexOutOfBoundsException
. Instead if
fromIndex < 0
or fromIndex + part.length() > text.length()
,
then this method returns false
.text
- the character sequence for which to tests for the presence of part
.fromIndex
- the offset in text
where to test for the presence of part
.part
- the part which may be present in text
.ignoreCase
- true
if the case should be ignored.true
if text
contains part
at the given offset
.NullPointerException
- if any of the arguments is null.String.regionMatches(boolean, int, String, int, int)
public static boolean startsWith(CharSequence text, CharSequence prefix, boolean ignoreCase)
true
if the given character sequence starts with the given prefix.text
- the characters sequence to test.prefix
- the expected prefix.ignoreCase
- true
if the case should be ignored.true
if the given sequence starts with the given prefix.NullPointerException
- if any of the arguments is null.public static boolean endsWith(CharSequence text, CharSequence suffix, boolean ignoreCase)
true
if the given character sequence ends with the given suffix.text
- the characters sequence to test.suffix
- the expected suffix.ignoreCase
- true
if the case should be ignored.true
if the given sequence ends with the given suffix.NullPointerException
- if any of the arguments is null.public static CharSequence commonPrefix(CharSequence s1, CharSequence s2)
null
, then the other text is returned.s1
- the first text, or null
.s2
- the second text, or null
.null
if both texts are null.public static CharSequence commonSuffix(CharSequence s1, CharSequence s2)
null
, then the other text is returned.s1
- the first text, or null
.s2
- the second text, or null
.null
if both texts are null.public static CharSequence token(CharSequence text, int fromIndex)
Let define c as the first non-blank character located at an index equals or greater than the given offset. Then the characters that are considered of the same type are:
Character.getType(int)
returns
the same value than for c.text
- the text for which to get the token.fromIndex
- index of the fist character to consider in the given text.text
starting at the given offset, or an empty string
if there is no non-blank character at or after the given offset.NullPointerException
- if the text
argument is null.public static CharSequence replace(CharSequence text, CharSequence toSearch, CharSequence replaceBy)
toSearch
is found in the given text or if toSearch
is equal to replaceBy
,
then this method returns the text
unchanged.
Otherwise this method returns a new character sequence with all occurrences replaced by replaceBy
.
This method is similar to String.replace(CharSequence, CharSequence)
except for the following:
CharSequence
objects.toSearch
value is searched verbatim.text
- the character sequence in which to perform the replacements, or null
.toSearch
- the string to replace.replaceBy
- the replacement for the searched string.text
if no replacement has been applied,
or null
if the given text was nullString.replace(char, char)
,
StringBuilders.replace(StringBuilder, String, String)
,
String.replace(CharSequence, CharSequence)
public static void copyChars(CharSequence src, int srcOffset, char[] dst, int dstOffset, int length)
char[]
array.src
- the characters sequence from which to copy characters.srcOffset
- index of the first character from src
to copy.dst
- the array where to copy the characters.dstOffset
- index where to write the first character in dst
.length
- number of characters to copy.String.getChars(int, int, char[], int)
,
StringBuilder.getChars(int, int, char[], int)
,
StringBuffer.getChars(int, int, char[], int)
,
CharBuffer.get(char[], int, int)
,
Segment.array
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.