# Why there is function: CharSequence#substring?

**URL:** <https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319>\
**Category:** Language Design\
**Created:** [September 4, 2018, 11:13am UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319 "2018-09-04T11:13:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [September 4, 2018, 11:13am UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319/1 "2018-09-04T11:13:44Z")

</div>

I spent a whole day to detect a strange hidden bug in Android app.

Caused by use **CharSequence#substring** instead of **String#substring** by accident. Thanks god I finally finished this pain.

I think this function named **substring** is confusing: it calls **subSequence** then **toString**.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [September 4, 2018, 11:50am UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319/2 "2018-09-04T11:50:16Z")

</div>

Why do you think `substring` defined this way is confusing? What was your bug with it?

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [September 4, 2018, 12:41pm UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319/3 "2018-09-04T12:41:30Z")

</div>

The bug is about custom span, .  
because SpannableString implements **CharSequence** , but sp.substring() is different to sp.toString().substring().  
Or in other words,  
sp.subSequence().toString() is confusing with sp.toString().substring().

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [September 4, 2018, 12:48pm UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319/4 "2018-09-04T12:48:12Z")

</div>

> [@lymoge](#):
>
> `sp.subSequence().toString()` is confusing with `sp.toString().substring()`

In what situations these two expressions can produce different result?

---

<div class="post-metadata">

**Author:** ![lymoge](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lymoge](https://discuss.kotlinlang.org/u/lymoge)\
**Post date:** [September 4, 2018, 12:57pm UTC](https://discuss.kotlinlang.org/t/why-there-is-function-charsequence-substring/9319/5 "2018-09-04T12:57:38Z")

</div>

Because the implements of subSequence is not about String.  
SpannableString may cause different effect sometimes, it’s strange. Always post measures to UI thread but nothing happens on screen.
