The CharSequence Interface
This interface defines a readable sequence of char values. It is implemented by the String and StringBuilder classes. Many methods in these classes accept arguments of this interface type, and specify it as their return type. This interface facilitates interoperability between these classes. It defines the following methods:
int length()
Returns the number of char values in this sequence.
default boolean isEmpty()
Returns true if this character sequence is empty. The default implementation returns the result of this.length() == 0. See also the String.isBlank() method (p. 453).
char charAt(int index)
A character at a particular index in a sequence can be read using the charAt() method. The first character is at index 0 and the last one at index 1 less than the number of characters in the string. If the index value is not valid, an IndexOutOfBoundsException is thrown.
CharSequence subSequence(int start, int end)
Returns a new CharSequence that is a subsequence of this sequence. Characters from the current sequence are read from the index start to the index end-1, inclusive.
String toString()
Returns a string containing the characters in this sequence in the same order as this sequence.
static int compare(CharSequence cs1, CharSequence cs2)
Compares two CharSequence instances lexicographically. It returns a negative value, 0, or a positive value if the first sequence is lexicographically less than, equal to, or greater than the second, respectively.
default IntStream chars()
This default method returns an IntStream of char values from this sequence (§16.4, p. 901).
Reading Characters from a String
The following methods can be used for character-related operations on a string:
char charAt(int index)
From the
CharSequence
interface
(p. 444).
char[] toCharArray()
Returns a new character array, with length equal to the length of this string, that contains the characters in this string.
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from the current string into the destination character array. Characters from the current string are read from index srcBegin to the index srcEnd-1, inclusive. They are copied into the destination array (dst), starting at index dstBegin and ending at index dstbegin+(srcEnd-srcBegin)-1. The number of characters copied is (srcEnd-srcBegin). An IndexOutOfBoundsException is thrown if the indices do not meet the criteria for the operation.
IntStream chars()
From the
CharSequence
interface
(p. 444).
int length()
From the
CharSequence
interface
(p. 444).
boolean isEmpty()
From the
CharSequence
interface
(p. 444).
Leave a Reply