Home  >  Article  >  Backend Development  >  golang replace r n

golang replace r n

PHPz
PHPzOriginal
2023-05-14 16:03:37610browse

Golang is an open source programming language that is widely used by many programmers and enterprises. When writing Golang code, you often need to use string operations. One common operation is to replace specific characters in a string, such as replace and
. This article will introduce how to use Golang to implement replacement and
methods.

Golang string

In Golang, a string is composed of a series of characters, each character is represented by UTF-8 encoding. Strings can be defined as literals using double quotes. For example:

var str string = "Hello, world!"

In a string, you can use backslash plus specific characters to represent some special characters, for example:

  • : represents a horizontal tab character (TAB)
  • : Represents carriage return (CR)
  • : Represents line feed (LF)

Therefore, in Golang, if a string contains and
characters, we can use the replacement operation to replace them with other characters or delete them. Below, we will introduce two methods to achieve this operation.

Method 1: strings.Replace()

Golang’s standard library strings provides a Replace() function, which can be used to replace substrings in strings. The declaration of this function is as follows:

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

Among them, s is the string to be operated on, old is the substring to be replaced, new is the substring to be replaced, n is the number of replacements, if n is less than 0, then Replace all matching substrings.

The following is a sample code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Helloworld
"
    newStr := strings.ReplaceAll(str, "
", "")
    fmt.Println(newStr)
}

In the above code, we introduced the strings package and called the ReplaceAll() function in the string to sum
Replaced with an empty string. The final output result is:

Helloworld

Method 2: Regular expression

In addition to using the Replace() function of strings, we can also use regular expressions to implement replacement operations. In Golang, the standard library regexp provides the Regexp type and some functions for regular expression matching and operations.

The following is a sample code:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    str := "Helloworld
"
    re, _ := regexp.Compile("[
]+")
    newStr := re.ReplaceAllString(str, "")
    fmt.Println(newStr)
}

In the above code, we construct a regular expression that matches all and
, and use the ReplaceAllString() function to replace the matched Partially replaced with an empty string. The final output is the same as the above example:

Helloworld

Summary

This article introduces two methods that can be used to replace the and
characters in Golang strings. The first is to use the Replace() function of the strings package, and the second is to use the regular expression of the regexp package. The results of both methods are the same, depending on the programmer's preference and the needs of the specific scenario.

The above is the detailed content of golang replace r n. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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