Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#pubctors) \| [Methods](https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#inhmethods)

# UnicodeSetSpanner

*** ** * ** ***


`
public


class
UnicodeSetSpanner
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.icu.text.UnicodeSetSpanner |

<br />

*** ** * ** ***

A helper class used to count, replace, and trim CharSequences based on UnicodeSet matches.
An instance is immutable (and thus thread-safe) iff the source UnicodeSet is frozen.

**Note:** The counting, deletion, and replacement depend on alternating a `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition` with
its inverse. That is, the code spans, then spans for the inverse, then spans, and so on.
For the inverse, the following mapping is used:

- `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#SIMPLE` \\u2192 `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#NOT_CONTAINED`
- `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#CONTAINED` \\u2192 `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#NOT_CONTAINED`
- `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#NOT_CONTAINED` \\u2192 `https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition#SIMPLE`

These are actually not complete inverses. However, the alternating works because there are no gaps. For example, with \[a{ab}{bc}\], you get the following behavior when scanning forward:

| SIMPLE | xxx\[ab\]cyyy |
| CONTAINED | xxx\[abc\]yyy |
| NOT_CONTAINED | \[xxx\]ab\[cyyy\] |
|---|---|

So here is what happens when you alternate:

| start | \|xxxabcyyy |
| NOT_CONTAINED | xxx\|abcyyy |
| CONTAINED | xxxabc\|yyy |
| NOT_CONTAINED | xxxabcyyy\| |
|---|---|

The entire string is traversed.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#UnicodeSetSpanner(android.icu.text.UnicodeSet)(https://developer.android.com/reference/android/icu/text/UnicodeSet source) ` Create a spanner from a UnicodeSet. |

| ### Public methods ||
|---|---|
| ` int` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#countIn(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence sequence) ` Returns the number of matching characters found in a character sequence, counting by CountMethod.MIN_ELEMENTS using SpanCondition.SIMPLE. |
| ` int` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#countIn(java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.CountMethod,%20android.icu.text.UnicodeSet.SpanCondition)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.CountMethod countMethod, https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition spanCondition) ` Returns the number of matching characters found in a character sequence. |
| ` int` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#countIn(java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.CountMethod)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.CountMethod countMethod) ` Returns the number of matching characters found in a character sequence, using SpanCondition.SIMPLE. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#deleteFrom(java.lang.CharSequence,%20android.icu.text.UnicodeSet.SpanCondition)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition spanCondition) ` Delete all matching spans in sequence, according to the spanCondition. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#deleteFrom(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence sequence) ` Delete all the matching spans in sequence, using SpanCondition.SIMPLE The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions. |
| ` boolean` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object other) ` Indicates whether some other object is "equal to" this one. |
| ` https://developer.android.com/reference/android/icu/text/UnicodeSet` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#getUnicodeSet()() ` Returns the UnicodeSet used for processing. |
| ` int` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#hashCode()() ` Returns a hash code value for the object. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#replaceFrom(java.lang.CharSequence,%20java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.CountMethod,%20android.icu.text.UnicodeSet.SpanCondition)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/java/lang/CharSequence replacement, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.CountMethod countMethod, https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition spanCondition) ` Replace all matching spans in sequence by replacement, according to the countMethod and spanCondition. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#replaceFrom(java.lang.CharSequence,%20java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/java/lang/CharSequence replacement) ` Replace all matching spans in sequence by the replacement, counting by CountMethod.MIN_ELEMENTS using SpanCondition.SIMPLE. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#replaceFrom(java.lang.CharSequence,%20java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.CountMethod)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/java/lang/CharSequence replacement, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.CountMethod countMethod) ` Replace all matching spans in sequence by replacement, according to the CountMethod, using SpanCondition.SIMPLE. |
| ` https://developer.android.com/reference/java/lang/CharSequence` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#trim(java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.TrimOption,%20android.icu.text.UnicodeSet.SpanCondition)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.TrimOption trimOption, https://developer.android.com/reference/android/icu/text/UnicodeSet.SpanCondition spanCondition) ` Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start or end of the string, depending on the trimOption and spanCondition. |
| ` https://developer.android.com/reference/java/lang/CharSequence` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#trim(java.lang.CharSequence,%20android.icu.text.UnicodeSetSpanner.TrimOption)(https://developer.android.com/reference/java/lang/CharSequence sequence, https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner.TrimOption trimOption) ` Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start or end of the string, using the trimOption and SpanCondition.SIMPLE. |
| ` https://developer.android.com/reference/java/lang/CharSequence` | ` https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner#trim(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence sequence) ` Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start and end of the string, using TrimOption.BOTH and SpanCondition.SIMPLE. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### UnicodeSetSpanner

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

```
public UnicodeSetSpanner (UnicodeSet source)
```

Create a spanner from a UnicodeSet. For speed and safety, the UnicodeSet should be frozen. However, this class
can be used with a non-frozen version to avoid the cost of freezing.

<br />

| Parameters ||
|---|---|
| `source` | `UnicodeSet`: the original UnicodeSet <br /> |

## Public methods

### countIn

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

```
public int countIn (CharSequence sequence)
```

Returns the number of matching characters found in a character sequence,
counting by CountMethod.MIN_ELEMENTS using SpanCondition.SIMPLE.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to count characters in <br /> |

| Returns ||
|---|---|
| `int` | the count. Zero if there are none. <br /> |

### countIn

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

```
public int countIn (CharSequence sequence, 
                UnicodeSetSpanner.CountMethod countMethod, 
                UnicodeSet.SpanCondition spanCondition)
```

Returns the number of matching characters found in a character sequence.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to count characters in <br /> |
| `countMethod` | `UnicodeSetSpanner.CountMethod`: whether to treat an entire span as a match, or individual elements as matches <br /> |
| `spanCondition` | `UnicodeSet.SpanCondition`: the spanCondition to use. SIMPLE or CONTAINED means only count the elements in the span; NOT_CONTAINED is the reverse. **WARNING:** when a UnicodeSet contains strings, there may be unexpected behavior in edge cases. <br /> |

| Returns ||
|---|---|
| `int` | the count. Zero if there are none. <br /> |

### countIn

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

```
public int countIn (CharSequence sequence, 
                UnicodeSetSpanner.CountMethod countMethod)
```

Returns the number of matching characters found in a character sequence, using SpanCondition.SIMPLE.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to count characters in <br /> |
| `countMethod` | `UnicodeSetSpanner.CountMethod`: whether to treat an entire span as a match, or individual elements as matches <br /> |

| Returns ||
|---|---|
| `int` | the count. Zero if there are none. <br /> |

### deleteFrom

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

```
public String deleteFrom (CharSequence sequence, 
                UnicodeSet.SpanCondition spanCondition)
```

Delete all matching spans in sequence, according to the spanCondition.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: charsequence to replace matching spans in. <br /> |
| `spanCondition` | `UnicodeSet.SpanCondition`: specify whether to modify the matching spans (CONTAINED or SIMPLE) or the non-matching (NOT_CONTAINED) <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | modified string. <br /> |

### deleteFrom

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

```
public String deleteFrom (CharSequence sequence)
```

Delete all the matching spans in sequence, using SpanCondition.SIMPLE
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: charsequence to replace matching spans in. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | modified string. <br /> |

### equals

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

```
public boolean equals (Object other)
```

Indicates whether some other object is "equal to" this one.


The `equals` method implements an equivalence relation
on non-null object references:

- It is *reflexive*: for any non-null reference value `x`, `x.equals(x)` should return `true`.
- It is *symmetric*: for any non-null reference values `x` and `y`, `x.equals(y)` should return `true` if and only if `y.equals(x)` returns `true`.
- It is *transitive*: for any non-null reference values `x`, `y`, and `z`, if `x.equals(y)` returns `true` and `y.equals(z)` returns `true`, then `x.equals(z)` should return `true`.
- It is *consistent*: for any non-null reference values `x` and `y`, multiple invocations of `x.equals(y)` consistently return `true` or consistently return `false`, provided no information used in `equals` comparisons on the objects is modified.
- For any non-null reference value `x`, `x.equals(null)` should return `false`.


An equivalence relation partitions the elements it operates on
into *equivalence classes*; all the members of an
equivalence class are equal to each other. Members of an
equivalence class are substitutable for each other, at least
for some purposes.

<br />

| Parameters ||
|---|---|
| `other` | `Object`: the reference object with which to compare. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if this object is the same as the obj argument; `false` otherwise. <br /> |

### getUnicodeSet

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

```
public UnicodeSet getUnicodeSet ()
```

Returns the UnicodeSet used for processing. It is frozen iff the original was.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/icu/text/UnicodeSet` | the construction set. <br /> |

### hashCode

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

```
public int hashCode ()
```

Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
`https://developer.android.com/reference/java/util/HashMap`.


The general contract of `hashCode` is:

- Whenever it is invoked on the same object more than once during an execution of a Java application, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` method, then calling the `hashCode` method on each of the two objects must produce the same integer result.
- It is *not* required that if two objects are unequal according to the `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` method, then calling the `hashCode` method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

<br />

<br />

| Returns ||
|---|---|
| `int` | a hash code value for this object. <br /> |

### replaceFrom

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

```
public String replaceFrom (CharSequence sequence, 
                CharSequence replacement, 
                UnicodeSetSpanner.CountMethod countMethod, 
                UnicodeSet.SpanCondition spanCondition)
```

Replace all matching spans in sequence by replacement, according to the countMethod and spanCondition.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: charsequence to replace matching spans in. <br /> |
| `replacement` | `CharSequence`: replacement sequence. To delete, use "" <br /> |
| `countMethod` | `UnicodeSetSpanner.CountMethod`: whether to treat an entire span as a match, or individual elements as matches <br /> |
| `spanCondition` | `UnicodeSet.SpanCondition`: specify whether to modify the matching spans (CONTAINED or SIMPLE) or the non-matching (NOT_CONTAINED) <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | modified string. <br /> |

### replaceFrom

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

```
public String replaceFrom (CharSequence sequence, 
                CharSequence replacement)
```

Replace all matching spans in sequence by the replacement,
counting by CountMethod.MIN_ELEMENTS using SpanCondition.SIMPLE.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: charsequence to replace matching spans in. <br /> |
| `replacement` | `CharSequence`: replacement sequence. To delete, use "" <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | modified string. <br /> |

### replaceFrom

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

```
public String replaceFrom (CharSequence sequence, 
                CharSequence replacement, 
                UnicodeSetSpanner.CountMethod countMethod)
```

Replace all matching spans in sequence by replacement, according to the CountMethod, using SpanCondition.SIMPLE.
The code alternates spans; see the class doc for `https://developer.android.com/reference/android/icu/text/UnicodeSetSpanner` for a note about boundary conditions.

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: charsequence to replace matching spans in. <br /> |
| `replacement` | `CharSequence`: replacement sequence. To delete, use "" <br /> |
| `countMethod` | `UnicodeSetSpanner.CountMethod`: whether to treat an entire span as a match, or individual elements as matches <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | modified string. <br /> |

### trim

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

```
public CharSequence trim (CharSequence sequence, 
                UnicodeSetSpanner.TrimOption trimOption, 
                UnicodeSet.SpanCondition spanCondition)
```

Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start or
end of the string, depending on the trimOption and spanCondition. For example:

    new UnicodeSet("[ab]").trim("abacatbab", TrimOption.LEADING, SpanCondition.SIMPLE)

... returns `"catbab"`.

<br />

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to trim <br /> |
| `trimOption` | `UnicodeSetSpanner.TrimOption`: LEADING, TRAILING, or BOTH <br /> |
| `spanCondition` | `UnicodeSet.SpanCondition`: SIMPLE, CONTAINED or NOT_CONTAINED <br /> |

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

### trim

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

```
public CharSequence trim (CharSequence sequence, 
                UnicodeSetSpanner.TrimOption trimOption)
```

Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start or
end of the string, using the trimOption and SpanCondition.SIMPLE. For example:

    new UnicodeSet("[ab]").trim("abacatbab", TrimOption.LEADING)

... returns `"catbab"`.

<br />

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to trim <br /> |
| `trimOption` | `UnicodeSetSpanner.TrimOption`: LEADING, TRAILING, or BOTH <br /> |

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

### trim

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

```
public CharSequence trim (CharSequence sequence)
```

Returns a trimmed sequence (using CharSequence.subsequence()), that omits matching elements at the start and
end of the string, using TrimOption.BOTH and SpanCondition.SIMPLE. For example:

    new UnicodeSet("[ab]").trim("abacatbab")

... returns `"cat"`.

<br />

<br />

| Parameters ||
|---|---|
| `sequence` | `CharSequence`: the sequence to trim <br /> |

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