0

I am trying to replace a specific position character from an array of strings. Here is what my code looks like:

package main

import (
    "fmt"
)

func main() {
    str := []string{"test","testing"}
    str[0][2] = 'y'
    fmt.Println(str)
}

Now, running this gives me the error:

cannot assign to str[0][2]

Any idea how to do this? I have tried using strings.Replace, but AFAIK it will replace all the occurrence of the given character, while I want to replace that specific character. Any help is appreciated. TIA.

icza
  • 389,944
  • 63
  • 907
  • 827
Pensu
  • 3,263
  • 10
  • 46
  • 71

2 Answers2

6

Strings in Go are immutable, you can't change their content. To change the value of a string variable, you have to assign a new string value.

An easy way is to first convert the string to a byte or rune slice, do the change and convert back:

s := []byte(str[0])
s[2] = 'y'
str[0] = string(s)
fmt.Println(str)

This will output (try it on the Go Playground):

[teyt testing]

Note: I converted the string to byte slice, because this is what happens when you index a string: it indexes its bytes. A string stores the UTF-8 byte sequence of the text, which may not necessarily map bytes to characters one-to-one.

If you need to replace the 2nd character, use []rune instead:

s := []rune(str[0])
s[2] = 'y'
str[0] = string(s)
fmt.Println(str)

In this example it doesn't matter though, but in general it may.

Also note that strings.Replace() does not (necessarily) replace all occurrences:

func Replace(s, old, new string, n int) string

The parameter n tells how many replacement are to be performed max. So the following also works (try it on the Go Playground):

str[0] = strings.Replace(str[0], "s", "y", 1)

Yet another solution could be to slice the string up until the replacable character, and starting from the character after the replacable one, and just concatenate them (try this one on the Go Playground):

str[0] = str[0][:2] + "y" + str[0][3:]

Care must be taken here too: the slice indices are byte indices, not character (rune) indices.

See related question: Immutable string and pointer address

icza
  • 389,944
  • 63
  • 907
  • 827
  • Or, depending on requirements, something like `str = str[:1] + "y" + str[2:]. If the string is large and the location of the change is known this can be more efficient. – Dave C Aug 01 '19 at 12:58
  • @DaveC Yes, except in this case indices should be bigger: `str[:2] + "y" + str[3:]` – icza Aug 01 '19 at 13:04
  • Thanks a lot @icza. – Pensu Aug 01 '19 at 14:33
1

Here's a function that will do that for you. It takes care of converting the string that you want to modify into a []rune, and then back out to string.

If your intention is to replace bytes rather than runes, you can:

  • copy this function's code, rename it from runeSub to byteSub
  • change the r rune parameter to b byte

Also available on repl.it

package main

import "fmt"

// runeSub - given an array of strings (ss), replace the
// (ri)th rune (character) in the (si)th string
// of (ss), with the rune (r)
//
// ss - the array of strings
// si - the index of the string in ss that you want to modify
// ri - the index of the rune in ss[si] that you want to replace
// r - the rune you want to insert
//
// NOTE: this function has no panic protection from things like
// out-of-bound index values
func runeSub(ss []string, si, ri int, r rune) {
  rr := []rune(ss[si])
  rr[ri] = r
  ss[si] = string(rr)
}

func main() {
  ss := []string{"test","testing"}
  runeSub(ss, 0, 2, 'y')
  fmt.Println(ss)
}
jefflunt
  • 33,527
  • 7
  • 88
  • 126