Home  >  Article  >  Backend Development  >  How to match and replace in golang

How to match and replace in golang

PHPz
PHPzOriginal
2023-04-23 10:05:49702browse

Golang is a programming language with high efficiency and powerful functions. One of its biggest advantages is that it has many built-in powerful libraries and tools and provides many convenient functions to process data. In this article, we will discuss string matching and replacement operations in Golang.

String matching

There are many functions in Golang that can be used for string matching operations, here are some of them:

  1. strings.Contains() function

Search for the specified substring in the string, and if found, returns a true value. Sample code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  substr1 := "hello"
  substr2 := "golang"
  
  fmt.Println(strings.Contains(str, substr1)) // true
  fmt.Println(strings.Contains(str, substr2)) // false
}
  1. strings.ContainsAny() function

Search for any character in the specified character set in the string, and if found, returns a true value. Sample code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  chars1 := "aeiou"
  chars2 := "1234"
  
  fmt.Println(strings.ContainsAny(str, chars1)) // true
  fmt.Println(strings.ContainsAny(str, chars2)) // false
}
  1. strings.HasPrefix() function

Determines whether the string starts with the specified prefix, and if so, returns a true value. Sample code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  prefix1 := "hello"
  prefix2 := "world"
  
  fmt.Println(strings.HasPrefix(str, prefix1)) // true
  fmt.Println(strings.HasPrefix(str, prefix2)) // false
}
  1. strings.HasSuffix() function

Determines whether the string ends with the specified suffix, and if so, returns a true value. Sample code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  suffix1 := "world"
  suffix2 := "hello"
  
  fmt.Println(strings.HasSuffix(str, suffix1)) // true
  fmt.Println(strings.HasSuffix(str, suffix2)) // false
}

String replacement

When we need to modify some parts of a string, the string replacement operation is very useful. There are several functions in Golang for string replacement operations.

  1. strings.Replace()

This function uses a new string to replace the substring in the original string. You can use the fourth parameter n to specify the replacement. frequency.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  newStr := strings.Replace(str, "world", "golang", -1)
  
  fmt.Println(newStr) // hello, golang
}

In this example, we replace "world" in the string with "golang".

  1. strings.ReplaceAll()

Same as strings.Replace() function, but this function will replace all occurrences in the original string with new ones String.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "hello, world"
  newStr := strings.ReplaceAll(str, "o", "O")
  
  fmt.Println(newStr) // hellO, wOrld
}

In this example, we replace all "o" in the string with "O".

  1. strings.Trim()

This function will delete the specified characters at both ends of the original string and return a new string. Sample code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "   hello, world!   "
  newStr := strings.Trim(str, " ")
  
  fmt.Println(newStr) // hello, world!
}

In this example, we delete the spaces at both ends of the string and return a new string.

Summary

In this article, we introduced string matching and replacement operations in Golang. I hope these sample codes can help you better understand the method of processing strings in Golang. Of course, there are many other string manipulation functions that can be used in Golang, and readers can further learn and master them.

The above is the detailed content of How to match and replace 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