×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strings.Replace() Function with Examples

Golang | strings.Replace() Function: Here, we are going to learn about the Replace() function of the strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 19, 2021

strings.Replace()

The Replace() function is an inbuilt function of strings package which is used to create a copy of the string with the first n non-overlapping occurrences (instances) of old substring replaced by new substring. It accepts four parameters – string, old substring to be replaced, new substring to replace with old substring, and the number of times to replace, and returns a new string after replacing the old substring with a new substring.

Note:

  • If the old substring is empty, it inserts the new substring at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
  • If the value of n is less than 0 (n<0), there is no limit for the number of replacements.

Syntax

func Replace(str, old_substr, new_substr string, n int) string

Parameters

  • str : The main string in which we have to make the replacements
  • old_substr : Old substring to be replaced
  • new_substr : New substring to replace with the old substring
  • n : Number of replacements

Return Value

The return type of Replace() function is a string, it returns a new string after replacing the old substring with a new substring.

Example 1

// Golang program to demonstrate the
// example of strings.Replace() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Replace("Hello, world! How are you?", "o", "oo", 2))
	fmt.Println(strings.Replace("Hello, world! How are you?", "o", "oo", -1))
	fmt.Println(strings.Replace("Hello, world! How are you?", "", "##", 2))
	fmt.Println(strings.Replace("Hello, world! How are you?", "", "##", -1))
	fmt.Println(strings.Replace("aabbccdd aabbppqq abcdef", "bb", "XX", -1))
	fmt.Println(strings.Replace("Is are am", " ", ". ", -1))
}

Output:

Helloo, woorld! How are you?
Helloo, woorld! Hoow are yoou?
##H##ello, world! How are you?
##H##e##l##l##o##,## ##w##o##r##l##d##!## ##H##o##w## ##a##r##e## ##y##o##u##?##
aaXXccdd aaXXppqq abcdef
Is. are. am

Example 2

// Golang program to demonstrate the
// example of strings.Replace() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "big black bug bit a big black dog on his big black nose"
	var old string
	var new string
	var n int

	old = "big"
	new = "Huge"
	n = -1
	new_string := strings.Replace(str, old, new, n)
	fmt.Println(new_string)

	// Now replacing within the new created string
	old = "black"
	new = "Red"
	n = -1
	new_string = strings.Replace(new_string, old, new, n)
	fmt.Println(new_string)
}

Output:

Huge black bug bit a Huge black dog on his Huge black nose
Huge Red bug bit a Huge Red dog on his Huge Red nose

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.