Kotlin: how to change character in String

I would like to change the line from "abcde" to "bcdea". So I wrote my code as below in Kotlin

var prevResult = "abcde" var tmp = prevResult[0] for (i in 0..prevResult.length - 2) { prevResult[i] = prevResult[i+1] // Error on preveResult[i] } prevResult[prevResult.length-1] = tmp // Error on preveResult[prevResult.lengt-1] 

It throws an error, as indicated above, in the comment line. What have I done wrong? How can I fix this and get what I want?

+6
source share
4 answers

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) // Don't really need tmp, use prevResult[0] instead. var result = builder.toString() 

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) 
+6
source

You can use drop(1) and first() (or take(1) ) to do this on one line:

 val str = "abcde" val r1 = str.drop(1) + str.first() val r2 = str.drop(1) + str.take(1) 

As for your code, Kotlin String is immutable and you cannot change its characters. To achieve what you want, you can convert String to a CharArray , change it, and then make String from it:

 val r1 = str.toCharArray().let { for (i in 0..it.lastIndex - 1) it[i] = it[i+1] it[it.lastIndex] = str[0] // str is unchanged String(it) } 

( let used for brevity to avoid creating additional variables)


Alternatively, you can write a more general version of this operation as an extension function for String :

 fun String.rotate(n: Int) = drop(n % length) + take(n % length) 

Using:

 val str = "abcde" val r1 = str.rotate(1) 
+4
source

Since the strings are immutable, you will have to copy the original string into an array, make changes to the array, and then create a new row from the modified array. Take a look:

  • getChars () to copy string characters to an array.
  • Run your algorithm on this array, making the necessary changes.
  • Convert the modified array back to String with String (char []) .
0
source

Simplified solution: just use toMutableList () to create a MutableList from Char, and then join everything together with joinToString.

Example:

Given the input of the string, we want to exchange characters at posA and posB:

 val chars = input.toMutableList() val temp = chars[posA] chars[posA] = chars[posB] chars[posB] = temp return chars.joinToString(separator = "") 
0
source

All Articles