Skip to main content

Command Palette

Search for a command to run...

Replacing part of the string

Updated
2 min readView as Markdown
Replacing part of the string
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources
  • Another very common task related to string processing is the replacement of the substring in a string. Go standard library provide the Replace function and Replacer type for the replacement of multiple strings at once.

Create the replace.go file with the following content:

        package main

        import (
         "fmt"
         "strings"
        )

        const refString = "Mary had a little lamb"
        const refStringTwo = "lamb lamb lamb lamb"

        func main() {
          out := strings.Replace(refString, "lamb", "wolf", -1)
          fmt.Println(out)

          out = strings.Replace(refStringTwo, "lamb", "wolf", 2)
          fmt.Println(out)
        }

output:

sangam:golang-daily sangam$ go run replace.go
Mary had a little wolf
wolf wolf lamb lamb
sangam:golang-daily sangam$

## Create the replacer.go file with the following content:
    package main

    import (
      "fmt"
      "strings"
    )

    const refString = "Mary had a little lamb"

    func main() {
      replacer := strings.NewReplacer("lamb", "wolf", "Mary", "Jack")
      out := replacer.Replace(refString)
      fmt.Println(out)
    }

output:

sangam:golang-daily sangam$ go run replacer.go Jack had a little wolf sangam:golang-daily sangam$

## Create the regexp.go file with the following content:
    package main

    import (
      "fmt"
      "regexp"
    )

    const refString = "Mary had a little lamb"

    func main() {
      regex := regexp.MustCompile("l[a-z]+")
      out := regex.ReplaceAllString(refString, "replacement")
      fmt.Println(out)
    }
output:

sangam:golang-daily sangam$ go run regexp.go Mary had a replacement replacement sangam:golang-daily sangam$

```

How it works...

  • The Replace function of a strings package is widely used for simple replacement. The last integer argument defines how many replacements will be done (in case of -1, all strings are replaced. See the second use of Replace, where only the first two occurrences are replaced.) The use of the Replace function is presented in steps 1 - 5.

  • Besides the Replace function, the Replacer structure also has the WriteString method. This method will write to the given writer with all replacements defined in Replacer. The main purpose of this type is its reusability. It can replace multiple strings at once and it is safe for concurrent use; see steps 6 - 8.

  • The more sophisticated method of replacing the substring, or even the matched pattern, is naturally the use of the regular expression. The Regex type pointer method ReplaceAllString could be leveraged for this purpose. Steps 9 - 11 illustrate the use of the regexp package. There's more...

  • If more complex logic for the replacement is needed, the regexp package is probably the one that should be used.

4 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !