Standard library / strings
strings.Replace()
Replaces up to n non-overlapping matches; negative n replaces all. Empty old matches at rune boundaries and at the start. Expected stdout: 'b-b-a\n'.
Formal Syntax & Structure
go syntaxgo
func Replace(s, old, new string, n int) stringParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | string | Input text or sequence. |
| old | string | Sequence to replace. |
| new | string | Replacement sequence. |
| n | int | Maximum result count or number of replacements, as specified. |
Return Value
string
Replaces up to n non-overlapping matches; negative n replaces all. Empty old matches at rune boundaries and at the start.
Working Example
Example Snippetgo
package main
import ( "fmt" "strings")
func main() { fmt.Println(string(strings.Replace("a-a-a", "a", "b", 2)))}