strings.Replace() Function in Golang With Examples Last Updated : 05 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report strings.Replace() Function in Golang is used to return a copy of the given string with the first n non-overlapping instances of old replaced by new one. Syntax: func Replace(s, old, new string, n int) string Here, s is the original or given string, old is the string that you want to replace. new is the string which replaces the old, and n is the number of times the old replaced. Note: If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements. Example 1: C // Golang program to illustrate the usage of // strings.Replace() function package main import ( "fmt" "strings" ) func main() { // using the function fmt.Println(strings.Replace("gfg gfg gfg", "g", "G", 3)) fmt.Println(strings.Replace("gfg gfg gfg", "fg", "FG", -1)) } Output: GfG Gfg gfg gFG gFG gFG In the first case, the first 3 matched substrings of "g" in "gfg gfg gfg" get replaced by "G". In the second case, every matched case of "fg" gets replaced by "FG". Example 2: Let's consider an example where we do not pass any value for old. CPP // Golang program to illustrate the usage of // strings.Replace() function package main import ( "fmt" "strings" ) func main() { // using the function fmt.Println(strings.Replace("i am geeks", "", "G", 5)) fmt.Println(strings.Replace("i love the geekiness", "", "F", -1)) } Output: GiG GaGmG geeks FiF FlFoFvFeF FtFhFeF FgFeFeFkFiFnFeFsFsF It can be seen that every alternate position gets replaced by new, n times. Comment More infoAdvertise with us Next Article strings.Replace() Function in Golang With Examples S supergops Follow Improve Article Tags : Go Language Write From Home Golang-String Similar Reads strings.NewReplacer() Function in Golang With Examples strings.NewReplacer() Function in Golang returns a new Replacer from a list of previous, new string sets. Substitutions are performed within the order they show up within the target string, without overlapping matches. The old string comparisons are done in argument order. The old string comparisons 1 min read reflect.String() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.String() Function in Golang is used to get the string v's underlying value, as a string. To access this function, 2 min read strings.Map() Function in Golang With Examples strings.Map() Function in Golang is used to return a copy of the string given string with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement. So whenever the user wants to make changes in a 2 min read strings.SplitN() Function in Golang with Examples strings.SplitN() Function() is a string manipulation function in Go language. It is used to split a given string into substrings separated by a separator. This function returns the slices of all substrings between those separators. Syntax: func SplitN(s, sep string, n int) []string Here, s is the st 3 min read strings.LastIndex() Function in Golang With Examples strings.LastIndex() function in the Golang returns the starting index of the occurrence of the last instance of a substring in a given string. If the substring is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the 2 min read Like