🔁 Go String Replacement – Replace Characters and Substrings Easily (2025 Guide)
🧲 Introduction – How to Replace Substrings in Go?
Go provides simple and powerful tools for string replacement using the strings
package. Whether you want to replace one or all occurrences, perform case-insensitive replacements, or manipulate Unicode strings, Go has it covered with strings.Replace()
and strings.ReplaceAll()
.
🎯 In this section, you’ll learn:
- How to use
strings.Replace()
andReplaceAll()
- Replace specific words, characters, or patterns
- Perform case-insensitive replacements
- Real-world examples like sanitizing input and text formatting
✅ Basic Syntax – strings.Replace()
import "strings"
text := "hello world"
result := strings.Replace(text, "world", "Go", 1)
fmt.Println(result)
📤 Output:
hello Go
✅ Replaces the first occurrence of "world"
with "Go"
.
🔁 Replace All Occurrences – strings.ReplaceAll()
import "strings"
text := "go go go!"
result := strings.ReplaceAll(text, "go", "run")
fmt.Println(result)
📤 Output:
run run run!
✅ Shorthand for replacing every match (equivalent to Replace(..., -1)
).
📌 Replace a Character
text := "a+b+c"
result := strings.ReplaceAll(text, "+", "-")
fmt.Println(result) // a-b-c
✅ Replaces all +
with -
.
🔁 Replace With Limit Using strings.Replace()
text := "one two one two one"
result := strings.Replace(text, "one", "1", 2)
fmt.Println(result)
📤 Output:
1 two 1 two one
✅ Limits replacement to the first 2 occurrences only.
🔠 Case-Insensitive Replacement (Manual)
Go’s Replace()
functions are case-sensitive. For case-insensitive behavior, convert both strings to lowercase:
import (
"strings"
)
text := "Go is Great. I love GO!"
lower := strings.ToLower(text)
modified := strings.ReplaceAll(lower, "go", "Golang")
fmt.Println(modified)
📤 Output:
golang is great. i love golang!
⚠️ Manual conversion changes the case of the entire string.
🧩 Advanced Replacements – Using regexp
Package
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\d+`)
result := re.ReplaceAllString("User123 has ID456", "#")
fmt.Println(result)
}
📤 Output:
User# has ID#
✅ Use regexp.ReplaceAllString()
to replace patterns, like numbers or symbols.
🧠 Summary Table – Replacement Methods
Method | Description | Case-Sensitive | Supports Pattern |
---|---|---|---|
strings.Replace() | Replace with occurrence limit | ✅ Yes | ❌ No |
strings.ReplaceAll() | Replace all occurrences | ✅ Yes | ❌ No |
regexp.ReplaceAllString() | Regex-based replacement | ✅ Yes | ✅ Yes |
📌 Summary – Recap & Next Steps
Go makes string replacement fast and easy through the strings
and regexp
packages. Whether you’re replacing characters, whole words, or dynamic patterns, you have tools for precise and performant transformations.
🔍 Key Takeaways:
- Use
Replace()
for controlled, count-limited replacements - Use
ReplaceAll()
to replace every instance of a string - Use
regexp.ReplaceAllString()
for pattern-based replacements - Go’s replacements are case-sensitive by default
⚙️ Next: Learn Go String Formatting for structured output and dynamic content rendering.
❓ FAQs – Go String Replacement
❓ How do I replace all instances of a string in Go?
✅ Use strings.ReplaceAll(str, old, new)
.
❓ Can I replace only the first N occurrences?
✅ Yes. Use strings.Replace(str, old, new, n)
and set your desired count.
❓ Is Go’s string replacement case-sensitive?
✅ Yes. Use strings.ToLower()
or regex with flags for case-insensitive alternatives.
❓ Can I replace substrings using regex in Go?
✅ Yes. Use regexp.ReplaceAllString()
from the regexp
package.
❓ What’s the difference between Replace and ReplaceAll?
✅ Replace()
gives control over how many replacements are made; ReplaceAll()
replaces every match.
Share Now :