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

Replace characters in string in golang

WBOY
Release: 2024-02-09 18:09:07
forward
1255 people have browsed it

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)
}
Copy after login

Now, running this command gives an error:

cannot assign to str[0][2]
Copy after login

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)
Copy after login

This will output (try it on Go Playground):

[teyt testing]
Copy after login

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)
Copy after login

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
Copy after login

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)
Copy after login

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:]
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template