Home >Backend Development >Golang >How to do find and replace operations in Golang

How to do find and replace operations in Golang

PHPz
PHPzOriginal
2023-04-14 11:38:221791browse

Golang is an open source statically typed programming language developed by Google. Golang has many advantages in syntax and performance, so it is very popular among developers. In Golang, find and replace are frequently used operations, so this article will introduce how to perform find and replace operations in Golang.

1. Search

  1. Contains

In Golang, the strings package provides multiple string manipulation functions, one of which is the Contains function. This function can be used to determine whether a string contains another string.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana"
    substr := "banana"
    fmt.Println(strings.Contains(str, substr)) // true
}
  1. Index

The Index function can return the position of the first occurrence of a string in another string. If not present, -1 is returned.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana"
    substr := "banana"
    fmt.Println(strings.Index(str, substr)) // 6
}
  1. Count

The Count function can return the number of times a string appears in another string.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana apple apple"
    substr := "apple"
    fmt.Println(strings.Count(str, substr)) // 3
}

2. Replacement

  1. Replace

The Replace function is used to replace all specified characters in the string character.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana"
    oldSubstring := "apple"
    newSubstring := "orange"
    newStr := strings.Replace(str, oldSubstring, newSubstring, -1)
    fmt.Println(newStr) // orange banana
}
  1. ReplaceAll

The ReplaceAll function is also used to replace all specified characters in a string.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana"
    oldSubstring := "apple"
    newSubstring := "orange"
    newStr := strings.ReplaceAll(str, oldSubstring, newSubstring)
    fmt.Println(newStr) // orange banana
}
  1. ReplaceN

The ReplaceN function is used to replace the first n specified characters in a string.

The sample code is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple banana apple"
    oldSubstring := "apple"
    newSubstring := "orange"
    newStr := strings.ReplaceN(str, oldSubstring, newSubstring, 1)
    fmt.Println(newStr) // orange banana apple
}

Summary

This article introduces common string functions for search and replace operations in Golang. These functions can help us process strings more conveniently and improve the efficiency and readability of the code.

The above is the detailed content of How to do find and replace operations in Golang. 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