https://kotlinlang.org logo
Title
m

Mark

06/01/2020, 2:41 AM
Are these two equivalent (just checking for any gotchas)?
if (text.length < 40) {
    // left pad with zeros
    text = String(CharArray(40 - text.length)).replace('\u0000', '0') + text
}
text = text.padStart(40, '0')
m

Matteo Mirk

06/01/2020, 7:19 AM
yes, it seems equivalent. You can check the standard lib source: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/text/Strings.kt#L154
👍 1
But why would you want to reimplement that?
m

Mark

06/01/2020, 7:20 AM
Just converting over some old Java code
👍 1
m

Matteo Mirk

06/01/2020, 7:54 AM
Did you know that IntelliJ can try and convert code from Java to Kotlin? The output is mechanical, works good for most cases or to get an idea of how it can be written in Kotlin, but it’s not idiomatic. Some times you have to clean it afterwards or just rewrite it by hand.
m

Mark

06/01/2020, 7:56 AM
I used the converter in AndroidStudio and the first snippet above, is what it spat out.
m

Matteo Mirk

06/01/2020, 7:57 AM
haha nice! Of course it’s not so smart to find a stdlib function to replace your old code… but sometimes it will