# StringBuilder does not compile in JS

**URL:** <https://discuss.kotlinlang.org/t/stringbuilder-does-not-compile-in-js/7831>\
**Category:** JavaScript\
**Created:** [May 17, 2018, 12:52pm UTC](https://discuss.kotlinlang.org/t/stringbuilder-does-not-compile-in-js/7831 "2018-05-17T12:52:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomschrot](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tomschrot/32/4346_2.png) [@tomschrot](https://discuss.kotlinlang.org/u/tomschrot)\
**Post date:** [May 17, 2018, 12:52pm UTC](https://discuss.kotlinlang.org/t/stringbuilder-does-not-compile-in-js/7831/1 "2018-05-17T12:52:10Z")

</div>

Hi

Im using StringBuilder.delete that does compile & run in the cli.  
The same code will not compile for Kotlin-JS, delete is not existing here!  
Instead, the use of removeRange is suggested but that has complete different behavior. Thus i can not use the code without re writing it completely.

Any hints on that?

---

<div class="post-metadata">

**Author:** ![Konstantin-Khvan](https://avatars.discourse-cdn.com/v4/letter/k/4af34b/32.png) [@Konstantin-Khvan](https://discuss.kotlinlang.org/u/Konstantin-Khvan)\
**Post date:** [May 17, 2018, 6:07pm UTC](https://discuss.kotlinlang.org/t/stringbuilder-does-not-compile-in-js/7831/2 "2018-05-17T18:07:29Z")

</div>

I have just took quick look at Java implementation of StringBuilder and it is basically `ArrayList<Char>`. Somehow I always thought it was `ArrayList<String>` that is getting concatenated to single String in toString().  
I would suggest implementing fully mutable custom StringBuilder and using it since kotlin implementation for some reason allows only appends.

It is easy to make. Took me about 7mins

```
class AwesomeBuilder {
    // this is not optimal. use js arrays instead of mutableList.
    private var chars = mutableListOf<Char>()

    val length = chars.size

    fun append(value: Int) = append(value.toString())
    fun append(str: String) = chars.addAll(str.asIterable())

    fun delete(startIndex: Int, endIndex: Int) {
        if (endIndex < startIndex)
            throw IndexOutOfBoundsException("End index ($endIndex) is less than start index ($startIndex).")

        if (endIndex == startIndex)
            return

        chars = mutableListOf<Char>().apply {
            addAll(chars.subList(0, startIndex))
            addAll(chars.subList(endIndex, length))
        }
    }

    override fun toString(): String {
        // this is really slow.
        return chars.joinToString(separator = "")
    }
}

```
