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

How to do find and replace operations in Golang

PHPz
Release: 2023-04-14 11:42:16
Original
1800 people have browsed it

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

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

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!

source:php.cn
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