Does Kotlin support mutable Strings?

Viewed 467

The following code doesn't work in Kotlin:

var s: String = "hello"
s[1]='a'

Does Kotlin have a mutable string type or do I have to use an Array<Char> if I want to be able to edit?

1 Answers

No, it doesn't.

You can use a StringBuilder to manipulate it:

val sb = StringBuilder("hello")
sb[1] = 'a'
println(sb.toString())

hallo

Related