# String.padEnd(length: Int) and multi code point characters

**URL:** <https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138>\
**Category:** Libraries\
**Created:** [January 30, 2020, 5:36pm UTC](https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138 "2020-01-30T17:36:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielAsher](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@DanielAsher](https://discuss.kotlinlang.org/u/DanielAsher)\
**Post date:** [January 30, 2020, 5:36pm UTC](https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138/1 "2020-01-30T17:36:46Z")

</div>

I’ve noticed that `String.padEnd()` counts multi code point characters (e.g. some emoji) as multiple characters which leads to poor layout with fixed width fonts when padding.

Is this a known issue, or is there a workaround?

thanks,

Daniel

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [January 30, 2020, 11:52pm UTC](https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138/3 "2020-01-30T23:52:25Z")

</div>

I’m guessing your on the JVM. This is a problem with the java String implementation. While it has no problems saving multi codepoint characters many of the utility functions don’t handle them correctly.  
I’m not sure about how complicated it would be to provide an alternate implementation.  
The problem is that the java String class represents characters as UTF-16 characters. This means any unicode character that is represented by more than 16 bits is saved as 2 separate `Char` values. This fact is ignored by many of the functions within String, eg. `String.lenght` does not return the number of unicode characters, it returns the number of 16bit characters within the `String`, some emoji counting for 2 characters. If your string also contains invisible characters like the unicode characters responsible for emoji skin color `String.lenght` might count a single displayed character as having a length of 4 or in some other combinations as even longer.  
There was an iPhone bug with a similar problem in 2015 that lead to your phone crashing when you recieved a special arabic text:

[![](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/d/dd4717a1fea2f9530eb7910a6cda4ab2d6000ad0.jpeg "The Effective Power Bug: Why Can Weird Text Crash Your iPhone?") ](https://www.youtube.com/watch?v=hJLMSllzoLA)

I don’t know of any workaround. It probably requires a complete reimplementation of `String` using a completely different memory system. `Char`s aren’t the best for manipulating strings outside of plain text. Maybe not use emoji 😜

---

<div class="post-metadata">

**Author:** ![DanielAsher](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@DanielAsher](https://discuss.kotlinlang.org/u/DanielAsher)\
**Post date:** [February 1, 2020, 6:16am UTC](https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138/4 "2020-02-01T06:16:41Z")

</div>

well I’m new to the JVM and Android development, coming from iOS, and I now understand why Android Studio has such a hard time with emoji 😩. By pushing all the complexity of non-plain text to clients, we as users, get a reading experience that is significantly worse than on native Mac/iOS apps. I understand that there is `android.icu.text` and `com.ibm.icu.text` but `String` is ubiquitous. Even if we adopt these libraries, unit testing on Android is hard as the JVM throws `Unimplemented Stub!` exceptions.

I honestly believe that kotlin has an opportunity to fix these poor user experiences. Perhaps an opt in compiler plugin that contains a correct default implementation of `kotlin.lang.String` and requires a conversion with `.toJavaString()` / `.fromJavaString()`.

Would the community comment on whether an issue on [GitHub - JetBrains/kotlin: The Kotlin Programming Language.](https://github.com/JetBrains/kotlin) would be worthwhile? Perhaps by 2022 we could have a uniform text experience in Android Studio and Android.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 1, 2020, 1:07pm UTC](https://discuss.kotlinlang.org/t/string-padend-length-int-and-multi-code-point-characters/16138/5 "2020-02-01T13:07:29Z")

</div>

The right place for issues is [https://kotl.in/issue](https://kotl.in/issue)  
That said this will probably require a KEEP: [GitHub - Kotlin/KEEP: Kotlin Evolution and Enhancement Process](https://github.com/Kotlin/KEEP/)

I’d be all for it, but this is something that will take a lot of work. My guess if we want something like this we would first need a KEEP as well as a prototype implementation of the String class. At that point it wouldn’t be to hard to implement a compiler plugin to incorporate literals or add this to the language directly.
