Home  >  Article  >  Backend Development  >  Replace characters in string in golang

Replace characters in string in golang

WBOY
WBOYforward
2024-02-09 18:09:071202browse

Replace characters in string in golang

In golang programming, replacing characters in a string is a common operation. Whether you are replacing a single character or multiple characters, you can do this by using the built-in string functions and methods. In this article, we will introduce to you how to replace string characters in golang by PHP editor Xinyi. Whether you're a beginner or an experienced developer, this article will provide you with concise and clear guidance to help you master this technique with ease.

Question content

I am trying to replace characters at specific positions in a string array. My code looks like this:

package main

import (
    "fmt"
)

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

Now, running this command gives an error:

cannot assign to str[0][2]

Any idea how to do this? I've tried using strings.Replace but as far as I know it will replace all occurrences of a given character and I want to replace that specific character. Any help is appreciated. TIA.

Solution

Strings in Go are immutable, you cannot change their contents. To change the value of a string variable, you must assign a new string value.

A simple way is to first convert the string to a bytes or rune slice, make changes and convert back:

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

This will output (try it on Go Playground):

[teyt testing]

NOTE: I converted the string to a byte slice because that's what happens when you index a string: it indexes its bytes. string A UTF-8 sequence of bytes that stores text, which does not necessarily map bytes to characters one-to-one.

If you need to replace the second character, please use []rune instead:

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

In this example it doesn't matter, but in general it probably does.

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

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

Parameters n tells the maximum number of replacements to be performed. Therefore, the following also works (try it on Go Playground):

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

Another solution might be to slice the string up to the replaceable character and start with the character after the replaceable character and then concatenate them (in Go to the playground):

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

Care must be taken here too: the slice index is a byte index, not a character (rune) index.

View related questions: Immutable strings and pointer addresses p>

The above is the detailed content of Replace characters in string in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete