Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/lang/CharSequence#pubmethods)

# CharSequence

` public interface CharSequence `   
` `

|---|
| java.lang.CharSequence |

Known indirect subclasses [AlteredCharSequence](https://developer.android.com/reference/android/text/AlteredCharSequence), [CharBuffer](https://developer.android.com/reference/java/nio/CharBuffer), [DateIntervalFormat.FormattedDateInterval](https://developer.android.com/reference/android/icu/text/DateIntervalFormat.FormattedDateInterval), [Editable](https://developer.android.com/reference/android/text/Editable), [FormattedNumber](https://developer.android.com/reference/android/icu/number/FormattedNumber), [FormattedNumberRange](https://developer.android.com/reference/android/icu/number/FormattedNumberRange), [FormattedValue](https://developer.android.com/reference/android/icu/text/FormattedValue), [GetChars](https://developer.android.com/reference/android/text/GetChars), [ListFormatter.FormattedList](https://developer.android.com/reference/android/icu/text/ListFormatter.FormattedList), [PrecomputedText](https://developer.android.com/reference/android/text/PrecomputedText), [RelativeDateTimeFormatter.FormattedRelativeDateTime](https://developer.android.com/reference/android/icu/text/RelativeDateTimeFormatter.FormattedRelativeDateTime), [Spannable](https://developer.android.com/reference/android/text/Spannable), [SpannableString](https://developer.android.com/reference/android/text/SpannableString), [SpannableStringBuilder](https://developer.android.com/reference/android/text/SpannableStringBuilder), [Spanned](https://developer.android.com/reference/android/text/Spanned), [SpannedString](https://developer.android.com/reference/android/text/SpannedString), [String](https://developer.android.com/reference/java/lang/String), [StringBuffer](https://developer.android.com/reference/java/lang/StringBuffer), [StringBuilder](https://developer.android.com/reference/java/lang/StringBuilder) [AlteredCharSequence](https://developer.android.com/reference/android/text/AlteredCharSequence) *This class was deprecated in API level 30. The functionality this class offers is easily implemented outside the framework.* [CharBuffer](https://developer.android.com/reference/java/nio/CharBuffer) A char buffer. [DateIntervalFormat.FormattedDateInterval](https://developer.android.com/reference/android/icu/text/DateIntervalFormat.FormattedDateInterval) An immutable class containing the result of a date interval formatting operation. [Editable](https://developer.android.com/reference/android/text/Editable) This is the interface for text whose content and markup can be changed (as opposed to immutable text like Strings). [FormattedNumber](https://developer.android.com/reference/android/icu/number/FormattedNumber) The result of a number formatting operation. [FormattedNumberRange](https://developer.android.com/reference/android/icu/number/FormattedNumberRange) The result of a number range formatting operation. [FormattedValue](https://developer.android.com/reference/android/icu/text/FormattedValue) An abstract formatted value: a string with associated field attributes. [GetChars](https://developer.android.com/reference/android/text/GetChars) Please implement this interface if your CharSequence has a getChars() method like the one in String that is faster than calling charAt() multiple times. [ListFormatter.FormattedList](https://developer.android.com/reference/android/icu/text/ListFormatter.FormattedList) An immutable class containing the result of a list formatting operation. [PrecomputedText](https://developer.android.com/reference/android/text/PrecomputedText) A text which has the character metrics data. [RelativeDateTimeFormatter.FormattedRelativeDateTime](https://developer.android.com/reference/android/icu/text/RelativeDateTimeFormatter.FormattedRelativeDateTime) Represents the result of a formatting operation of a relative datetime. [Spannable](https://developer.android.com/reference/android/text/Spannable) This is the interface for text to which markup objects can be attached and detached. [SpannableString](https://developer.android.com/reference/android/text/SpannableString) This is the class for text whose content is immutable but to which markup objects can be attached and detached. [SpannableStringBuilder](https://developer.android.com/reference/android/text/SpannableStringBuilder) This is the class for text whose content and markup can both be changed. [Spanned](https://developer.android.com/reference/android/text/Spanned) This is the interface for text that has markup objects attached to ranges of it. [SpannedString](https://developer.android.com/reference/android/text/SpannedString) This is the class for text whose content and markup are immutable. [String](https://developer.android.com/reference/java/lang/String) The `String` class represents character strings. [StringBuffer](https://developer.android.com/reference/java/lang/StringBuffer) A thread-safe, mutable sequence of characters. [StringBuilder](https://developer.android.com/reference/java/lang/StringBuilder) A mutable sequence of characters.   

*** ** * ** ***

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](https://developer.android.com/reference/java/lang/Character#unicode) for details.

This interface does not refine the general contracts of the `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` and `https://developer.android.com/reference/java/lang/Object#hashCode()` methods. The result of testing two objects that implement `CharSequence` for equality 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.

<br />

## Summary

| ### Public methods ||
|---|---|
| ` abstract char` | ` https://developer.android.com/reference/java/lang/CharSequence#charAt(int)(int index) ` Returns the `char` value at the specified index. |
| ` default https://developer.android.com/reference/java/util/stream/IntStream` | ` https://developer.android.com/reference/java/lang/CharSequence#chars()() ` Returns a stream of `int` zero-extending the `char` values from this sequence. |
| ` default https://developer.android.com/reference/java/util/stream/IntStream` | ` https://developer.android.com/reference/java/lang/CharSequence#codePoints()() ` Returns a stream of code point values from this sequence. |
| ` static int` | ` https://developer.android.com/reference/java/lang/CharSequence#compare(java.lang.CharSequence,%20java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence cs1, https://developer.android.com/reference/java/lang/CharSequence cs2) ` Compares two `CharSequence` instances lexicographically. |
| ` default void` | ` https://developer.android.com/reference/java/lang/CharSequence#getChars(int,%20int,%20char[],%20int)(int srcBegin, int srcEnd, char[] dst, int dstBegin) ` Copies characters from this sequence into the given destination array. |
| ` default boolean` | ` https://developer.android.com/reference/java/lang/CharSequence#isEmpty()() ` Returns `true` if this character sequence is empty. |
| ` abstract int` | ` https://developer.android.com/reference/java/lang/CharSequence#length()() ` Returns the length of this character sequence. |
| ` abstract https://developer.android.com/reference/java/lang/CharSequence` | ` https://developer.android.com/reference/java/lang/CharSequence#subSequence(int,%20int)(int start, int end) ` Returns a `CharSequence` that is a subsequence of this sequence. |
| ` abstract https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/CharSequence#toString()() ` Returns a string containing the characters in this sequence in the same order as this sequence. |

## Public methods

### charAt

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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](https://reference/java/lang/Character.html#unicode), the surrogate value is returned.

<br />

| Parameters ||
|---|---|
| `index` | `int`: the index of the `char` value to be returned <br /> |

| Returns ||
|---|---|
| `char` | the specified `char` value <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IndexOutOfBoundsException` | if the `index` argument is negative or not less than `length()` |

### chars

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public IntStream chars ()
```

Returns a stream of `int` zero-extending the `char` values from this sequence. Any char which maps to a [surrogate code point](https://reference/java/lang/Character.html#unicode) is passed through uninterpreted.

The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is [*late-binding*](https://developer.android.com/reference/java/util/Spliterator.html#binding)). If the sequence is modified during that operation then the result is undefined.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/stream/IntStream` | an IntStream of char values from this sequence <br /> |

### codePoints

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 [Character.toCodePoint](https://developer.android.com/reference/java/lang/Character#toCodePoint(char,%20char)) 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.

The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is [*late-binding*](https://developer.android.com/reference/java/util/Spliterator.html#binding)). If the sequence is modified during that operation then the result is undefined.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/stream/IntStream` | an IntStream of Unicode code points from this sequence <br /> |

### compare

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static int compare (CharSequence cs1, 
                CharSequence cs2)
```

Compares two `CharSequence` instances lexicographically. Returns a negative value, zero, or a positive value if the first sequence is lexicographically less than, equal to, or greater than the second, respectively.

The lexicographical ordering of `CharSequence` is defined as follows. Consider a `CharSequence` *cs* of length *len* to be a sequence of char values, *cs\[0\]* to *cs\[len-1\]*. Suppose *k* is the lowest index at which the corresponding char values from each sequence differ. The lexicographic ordering of the sequences is determined by a numeric comparison of the char values *cs1\[k\]* with *cs2\[k\]*. If there is no such index *k*, the shorter sequence is considered lexicographically less than the other. If the sequences have the same length, the sequences are considered lexicographically equal.

<br />

| Parameters ||
|---|---|
| `cs1` | `CharSequence`: the first `CharSequence` <br /> |
| `cs2` | `CharSequence`: the second `CharSequence` <br /> |

| Returns ||
|---|---|
| `int` | the value `0` if the two `CharSequence` are equal; a negative integer if the first `CharSequence` is lexicographically less than the second; or a positive integer if the first `CharSequence` is lexicographically greater than the second. <br /> |

### getChars

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void getChars (int srcBegin, 
                int srcEnd, 
                char[] dst, 
                int dstBegin)
```

Copies characters from this sequence into the given destination array. The first character to be copied is at index `srcBegin`; the last character to be copied is at index `srcEnd-1`. The total number of characters to be copied is `srcEnd-srcBegin`. The characters are copied into the subarray of `dst` starting at index `dstBegin` and ending at index:

    dstbegin + (srcEnd-srcBegin) - 1

<br />

<br />

##### Implementation Requirements:

- The default implementation invokes `https://developer.android.com/reference/java/lang/CharSequence#charAt(int)` in a loop iterating `index` from `srcBegin` to `srcEnd-1`. Concurrent truncation of this character sequence can throw `IndexOutOfBoundsException`. In this case, some characters, but not all, may be already transferred.

| Parameters ||
|---|---|
| `srcBegin` | `int`: start copying at this offset. <br /> |
| `srcEnd` | `int`: stop copying at this offset. <br /> |
| `dst` | `char`: the array to copy the data into. <br /> |
| `dstBegin` | `int`: offset into `dst`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IndexOutOfBoundsException` | if any of the following is true: - `srcBegin` is negative - `dstBegin` is negative - the `srcBegin` argument is greater than the `srcEnd` argument. - `srcEnd` is greater than `this.length()`. - `dstBegin+srcEnd-srcBegin` is greater than `dst.length` |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `dst` is `null` |

### isEmpty

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isEmpty ()
```

Returns `true` if this character sequence is empty.

<br />

##### Implementation Requirements:

- The default implementation returns the result of calling `length() == 0`.

| Returns ||
|---|---|
| `boolean` | `true` if `https://developer.android.com/reference/java/lang/CharSequence#length()` is `0`, otherwise `false` <br /> |

### length

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int length ()
```

Returns the length of this character sequence. The length is the number of 16-bit `char`s in the sequence.

<br />

| Returns ||
|---|---|
| `int` | the number of `char`s in this sequence <br /> |

### subSequence

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `start` | `int`: the start index, inclusive <br /> |
| `end` | `int`: the end index, exclusive <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/CharSequence` | the specified subsequence <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IndexOutOfBoundsException` | if `start` or `end` are negative, if `end` is greater than `length()`, or if `start` is greater than `end` |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string consisting of exactly this sequence of characters <br /> |