Search and Replace in a String

· 174 words · 1 minute read

As programmers we often need to take a string and replace parts of it with something else. The code below has three examples, first of which is a basic ‘find all’ and replace, the second changes only the first occurrence of ‘sound’ and finally the third example demonstrates how to change a string containing quotes to use escaped quotes. These are changed by using the 4th argument to define how many times to replace, with -1 being everytime.

All this functionality is managed by the strings package and the Replace function (view docs).

package main

import (
    "fmt"
    "strings"
)

func main() {

    // Example 1: Willkommen to GoLangCode.com
    myText := "Welcome to GoLangCode.com"
    myText = strings.Replace(myText, "Welcome", "Willkommen", -1)
    fmt.Println(myText)

    // Example 2: Change first occurance
    // Output: The car sounds sound
    myText = "The sound sounds sound"
    myText = strings.Replace(myText, "sound", "car", 1)
    fmt.Println(myText)

    // Example 3: Replacing quotes (double backslash needed)
    // Output: I \'quote\' this text
    myText = "I 'quote' this text"
    myText = strings.Replace(myText, "'", "\\'", -1)
    fmt.Println(myText)
}

search and replace within string in golang

Image of Author Edd Turtle

Author: Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!