Replace()
The strings.Replace()
method replaces all, or a number of, occurrences of a specified substring within a given string with another substring. It returns a new string with the replacements made.
Syntax
func Replace(provided str, old str, new str, n int) string
If the old
string is empty, it corresponds to the beginning of the provided
string. It returns up to k+1 replacements after each UTF-8 sequence, resulting in a k-rune string. If n
is a negative integer, there is no limit to the number of substitutions that can be made.
The strings.Replace()
function takes four parameters:
provided
: The original string.old
: The string that will be replaced.new
: The string that replaces theold
string.n
: The count of replacements made of theold
string.
Example
The following example shows the implementation of strings.Replace()
:
package mainimport("fmt""strings")func main(){fmt.Println(strings.Replace("codecademy CODEcademy codecademy", "code", "</> ", -1))fmt.Println(strings.Replace("empty old string", "", "!", 2))}
The output will be:
</> cademy CODEcademy </> cademy!e!mpty old string
Codebyte Example
The provided example is executable and demonstrates the usage of the strings.Replace()
function. It replaces occurrences of the old string "one"
with the new string "1"
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.