CharSequence
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
A CharSequence is a readable sequence of char
values. This
interface provides uniform, read-only access to many different kinds of
char
sequences.
A char
value represents a character in the Basic
Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.
This interface does not refine the general contracts of the equals
and hashCode
methods. The result of comparing two
objects that implement CharSequence is therefore, in general,
undefined. Each object may be implemented by a different class, and there
is no guarantee that each class will be capable of testing its instances
for equality with those of the other. It is therefore inappropriate to use
arbitrary CharSequence instances as elements in a set or as keys in
a map.
Public Method Summary
abstract
char
|
charAt(int index)
Returns the char value at the specified index.
|
IntStream
|
chars()
Returns a stream of int zero-extending the char values
from this sequence.
|
IntStream
|
codePoints()
Returns a stream of code point values from this sequence.
|
abstract
int
|
length()
Returns the length of this character sequence.
|
abstract
CharSequence
|
subSequence(int start, int end)
Returns a CharSequence that is a subsequence of this sequence.
|
abstract
String
|
toString()
Returns a string containing the characters in this sequence in the same
order as this sequence.
|
Public Methods
public
abstract
char
charAt
(int index)
Returns the char
value at the specified index. An index ranges from zero
to length() - 1. The first char
value of the sequence is at
index zero, the next at index one, and so on, as for array
indexing.
If the char
value specified by the index is a
surrogate, the surrogate
value is returned.
Parameters
index |
the index of the char value to be returned |
Returns a stream of int
zero-extending the char
values
from this sequence. Any char which maps to a surrogate code
point is passed through uninterpreted.
If the sequence is mutated while the stream is being read, the
result is undefined.
Returns
- an IntStream of char values from this sequence
public
IntStream
codePoints
()
Returns a stream of code point values from this sequence. Any surrogate
pairs encountered in the sequence are combined as if by {@linkplain Character#toCodePoint Character.toCodePoint} and the result is passed
to the stream. Any other code units, including ordinary BMP characters,
unpaired surrogates, and undefined code units, are zero-extended to
int
values which are then passed to the stream.
If the sequence is mutated while the stream is being read, the result
is undefined.
Returns
- an IntStream of Unicode code points from this sequence
public
abstract
int
length
()
Returns the length of this character sequence. The length is the number
of 16-bit char
s in the sequence.
Returns
- the number of
char
s in this sequence
public
abstract
CharSequence
subSequence
(int start, int end)
Returns a CharSequence
that is a subsequence of this sequence.
The subsequence starts with the char
value at the specified index and
ends with the char
value at index end - 1. The length
(in char
s) of the
returned sequence is end - start, so if start == end
then an empty sequence is returned.
Parameters
start |
the start index, inclusive |
end |
the end index, exclusive |
Returns
- the specified subsequence
public
abstract
String
toString
()
Returns a string containing the characters in this sequence in the same
order as this sequence. The length of the string will be the length of
this sequence.
Returns
- a string consisting of exactly this sequence of characters
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eCharSequence\u003c/code\u003e interface provides read-only access to a sequence of \u003ccode\u003echar\u003c/code\u003e values, representing characters in the Basic Multilingual Plane (BMP) or surrogates.\u003c/p\u003e\n"],["\u003cp\u003eCommon implementations include \u003ccode\u003eString\u003c/code\u003e, \u003ccode\u003eStringBuffer\u003c/code\u003e, \u003ccode\u003eStringBuilder\u003c/code\u003e, and \u003ccode\u003eCharBuffer\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eKey methods allow retrieving individual characters (\u003ccode\u003echarAt\u003c/code\u003e), subsequences (\u003ccode\u003esubSequence\u003c/code\u003e), the sequence length (\u003ccode\u003elength\u003c/code\u003e), and converting the sequence to a \u003ccode\u003eString\u003c/code\u003e (\u003ccode\u003etoString\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eCharSequence\u003c/code\u003e also offers methods (\u003ccode\u003echars\u003c/code\u003e, \u003ccode\u003ecodePoints\u003c/code\u003e) to create streams of \u003ccode\u003eint\u003c/code\u003e values representing the characters or code points within the sequence.\u003c/p\u003e\n"],["\u003cp\u003eComparing two \u003ccode\u003eCharSequence\u003c/code\u003e objects using \u003ccode\u003eequals\u003c/code\u003e or \u003ccode\u003ehashCode\u003c/code\u003e generally yields undefined results due to potential implementation differences.\u003c/p\u003e\n"]]],["`CharSequence` provides read-only access to `char` sequences. Key actions include: retrieving a `char` value at a specific index (`charAt`), getting the sequence's length (`length`), obtaining a subsequence (`subSequence`), and converting the sequence to a string (`toString`). It also provides the option to return a stream of `char` values (`chars`) or code point values (`codePoints`). The implementation doesn't refine `equals` and `hashCode` method contracts, so comparison results are undefined.\n"],null,["# CharSequence\n\npublic interface **CharSequence** \n\n|---|---|---|\n| Known Indirect Subclasses [CharBuffer](../../../reference/java/nio/CharBuffer.html), [String](../../../reference/java/lang/String.html), [StringBuffer](../../../reference/java/lang/StringBuffer.html), [StringBuilder](../../../reference/java/lang/StringBuilder.html) |------------------------------------------------------------------|--------------------------------------------------| | [CharBuffer](../../../reference/java/nio/CharBuffer.html) | A char buffer. | | [String](../../../reference/java/lang/String.html) | The `String` class represents character strings. | | [StringBuffer](../../../reference/java/lang/StringBuffer.html) | A thread-safe, mutable sequence of characters. | | [StringBuilder](../../../reference/java/lang/StringBuilder.html) | A mutable sequence of characters. | |||\n\nA CharSequence is a readable sequence of `char` values. This\ninterface provides uniform, read-only access to many different kinds of\n`char` sequences.\nA `char` value represents a character in the *Basic\nMultilingual Plane (BMP)* or a surrogate. Refer to [Unicode Character Representation](/j2objc/javadoc/jre/reference/java/lang/Character#unicode) for details.\n\nThis interface does not refine the general contracts of the [equals](../../../reference/java/lang/Object.html#equals(java.lang.Object)) and [hashCode](../../../reference/java/lang/Object.html#hashCode()) methods. The result of comparing two\nobjects that implement CharSequence is therefore, in general,\nundefined. Each object may be implemented by a different class, and there\nis no guarantee that each class will be capable of testing its instances\nfor equality with those of the other. It is therefore inappropriate to use\narbitrary CharSequence instances as elements in a set or as keys in\na map.\n\n\u003cbr /\u003e\n\n### Public Method Summary\n\n|-------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract char | [charAt](../../../reference/java/lang/CharSequence.html#charAt(int))(int index) Returns the `char` value at the specified index. |\n| [IntStream](../../../reference/java/util/stream/IntStream.html) | [chars](../../../reference/java/lang/CharSequence.html#chars())() Returns a stream of `int` zero-extending the `char` values from this sequence. |\n| [IntStream](../../../reference/java/util/stream/IntStream.html) | [codePoints](../../../reference/java/lang/CharSequence.html#codePoints())() Returns a stream of code point values from this sequence. |\n| abstract int | [length](../../../reference/java/lang/CharSequence.html#length())() Returns the length of this character sequence. |\n| abstract [CharSequence](../../../reference/java/lang/CharSequence.html) | [subSequence](../../../reference/java/lang/CharSequence.html#subSequence(int,%20int))(int start, int end) Returns a `CharSequence` that is a subsequence of this sequence. |\n| abstract [String](../../../reference/java/lang/String.html) | [toString](../../../reference/java/lang/CharSequence.html#toString())() Returns a string containing the characters in this sequence in the same order as this sequence. |\n\nPublic Methods\n--------------\n\n#### public abstract char\n**charAt**\n(int index)\n\nReturns the `char` value at the specified index. An index ranges from zero\nto length() - 1. The first `char` value of the sequence is at\nindex zero, the next at index one, and so on, as for array\nindexing.\n\nIf the `char` value specified by the index is a\n[surrogate](../../..//java/lang/Character.html#unicode), the surrogate\nvalue is returned. \n\n##### Parameters\n\n| index | the index of the `char` value to be returned |\n|-------|----------------------------------------------|\n\n##### Returns\n\n- the specified `char` value \n\n##### Throws\n\n| [IndexOutOfBoundsException](../../../reference/java/lang/IndexOutOfBoundsException.html) | if the index argument is negative or not less than length() |\n|------------------------------------------------------------------------------------------|-------------------------------------------------------------|\n\n#### public [IntStream](../../../reference/java/util/stream/IntStream.html)\n**chars**\n()\n\nReturns a stream of `int` zero-extending the `char` values\nfrom this sequence. Any char which maps to a [surrogate code\npoint](../../..//java/lang/Character.html#unicode) is passed through uninterpreted.\n\nIf the sequence is mutated while the stream is being read, the\nresult is undefined. \n\n##### Returns\n\n- an IntStream of char values from this sequence \n\n#### public [IntStream](../../../reference/java/util/stream/IntStream.html)\n**codePoints**\n()\n\nReturns a stream of code point values from this sequence. Any surrogate\npairs encountered in the sequence are combined as if by {@linkplain Character#toCodePoint Character.toCodePoint} and the result is passed\nto the stream. Any other code units, including ordinary BMP characters,\nunpaired surrogates, and undefined code units, are zero-extended to\n`int` values which are then passed to the stream.\n\nIf the sequence is mutated while the stream is being read, the result\nis undefined. \n\n##### Returns\n\n- an IntStream of Unicode code points from this sequence \n\n#### public abstract int\n**length**\n()\n\nReturns the length of this character sequence. The length is the number\nof 16-bit `char`s in the sequence. \n\n##### Returns\n\n- the number of `char`s in this sequence \n\n#### public abstract [CharSequence](../../../reference/java/lang/CharSequence.html)\n**subSequence**\n(int start, int end)\n\nReturns a `CharSequence` that is a subsequence of this sequence.\nThe subsequence starts with the `char` value at the specified index and\nends with the `char` value at index end - 1. The length\n(in `char`s) of the\nreturned sequence is end - start, so if start == end\nthen an empty sequence is returned. \n\n##### Parameters\n\n| start | the start index, inclusive |\n| end | the end index, exclusive |\n|-------|----------------------------|\n\n##### Returns\n\n- the specified subsequence \n\n##### Throws\n\n| [IndexOutOfBoundsException](../../../reference/java/lang/IndexOutOfBoundsException.html) | if start or end are negative, if end is greater than length(), or if start is greater than end |\n|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|\n\n#### public abstract [String](../../../reference/java/lang/String.html)\n**toString**\n()\n\nReturns a string containing the characters in this sequence in the same\norder as this sequence. The length of the string will be the length of\nthis sequence. \n\n##### Returns\n\n- a string consisting of exactly this sequence of characters"]]