Strings in Kotlin , as in Java, are immutable, so there is no string.set(index, value) (which is equivalent to string[index] = value ).
To build a string from fragments, you can use StringBuilder , build CharSequence and use joinToString , work with a simple array ( char[] ) or do result = result + nextCharacter (each time it creates a new string - this is the most expensive way).
Here you can do it with StringBuilder :
var prevResult = "abcde" var tmp = prevResult[0] var builder = StringBuilder() for (i in 0..prevResult.length - 2) { builder.append(prevResult[i+1]) } builder.append(tmp)
However, a much simpler way to achieve your goal ("bcdea" from "abcde") is to simply "move" one character:
var result = prevResult.substring(1) + prevResult[0]
or using the Sequence methods:
var result = prevResult.drop(1) + prevResult.take(1)
source share