How to replace nth char from a string in Go

Strings are immutable.

chars = chars[:3] + "z" + chars[4:]

Using the index operator [] on a string yields byte:

A string's bytes can be accessed by integer indices 0 through len(s)-1.

so the naive approach of indexing the string directly will work only if it just happens to contain only single-byte characters.

A more solid solution is to convert the string to rune slice:

func replaceAt(s string, i int, c rune) string {
    r := []rune(s)
    r[i] = c
    return string(r)
}

Which will work nicely with arbitrary UTF-8 input:

package main

import "fmt"

var chars = "abcdef"
var multibyte = "由汉字组成的一句话"

func main() {
    v := replaceAt(chars, 3, 'z')
    fmt.Println(v) // abczef

    w := replaceAt(multibyte, 3, 'z')
    fmt.Println(w) // 由汉子z成的一句话
}

func replaceAt(s string, i int, c rune) string {
    r := []rune(s)
    r[i] = c
    return string(r)
}

Playground: https://go.dev/play/p/wKtYIkIXw4Z


This is happening because chars is actually a string and is immutable. If you declared it appropriately (as a byte slice) then you can assign to it as you're attempting. Here's an example;

package main
import "fmt"

func main() {
  var chars = []byte{'a', 'b', 'c', 'd', 'e', 'f'}
  fmt.Println(string(chars[3]))
  fmt.Printf("%T\n", chars)
  chars[3] = 'z'
  fmt.Println(string(chars))
}

https://play.golang.org/p/N1sSsfIBQY

Alternately you could use reslicing as demonstrated in the other answer.


Use slice indexing to remove the character at the index, and place a new character there instead.

package main
import "fmt"

func main() {
  var chars = "abcdef"
  fmt.Println(string(chars[3]))
  chars = chars[:3] + "z" + chars[3+1:]
  fmt.Println(string(chars[3]))
}

Output:

d
z

[:3] selects everything in the slice from the beginning up until the index 3, and [3+1:] selects everything from the index (3+1) until the end of the slice. Put the character you wanted in-between the two statements and cat them all together for the effect of replacing a character at a specific index.

If you want to replace a specific character (i.e. all (or some of) the instances of the letter 'b') you can use the strings.Replace function.

Tags:

String

Replace

Go