How to replace string in go language
In the Go language, you can use the Replace() function of the strings package to replace a string. The syntax is "strings.Replace(original string, value to be searched, replacement value, number of replacements)"; if you replace If the number of times is negative, it means that all specified substrings in the string will be replaced with new values.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
During the development process, sometimes we need to replace a specific string in a string with a new string. In the Go language, it is necessary to replace a certain string with a new string. Requirements, we can achieve it through the strings.Replace() function.
strings.Replace() function
Syntax
func Replace(s, old, new string, n int) string
Parameters | Description |
---|---|
s | The entire string to be replaced. |
old | The string to be replaced. What string should |
new | be replaced with. |
n | The number of times to be replaced, -1, then all old in the string s will be replaced with new. |
Return value
Returns the replaced string.
Explanation
Replace the old string in string s with the new string, replace n times, Returns the replaced string. If n is -1, then all old in string s will be replaced with new.
Usage example:
##Replace the string once
package main import ( "fmt" "strings" ) func main() { //使用 strings.Replace() 函数,替换字符串 strHaiCoder := "hello你好hello" fmt.Println("StrReplace =", strings.Replace(strHaiCoder, "hello", "hi", 1)) }
Replace string multiple times
package main import ( "fmt" "strings" ) func main() { //使用 strings.Replace() 函数,替换字符串 strHaiCoder := "hello你好hello" fmt.Println("StrReplace =", strings.Replace(strHaiCoder, "hello", "hi", 2)) }
Replace all strings
package main import ( "fmt" "strings" ) func main() { //使用 strings.Replace() 函数,替换字符串 strHaiCoder := "hello你好hello你好hello你好hello你好hello" fmt.Println("StrReplace =", strings.Replace(strHaiCoder, "hello", "hi", -1)) }
Go video tutorial, Programming teaching】
The above is the detailed content of How to replace string in go language. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...
