- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to search and replace texts in a string in Golang?
We often want to replace certain strings or all the strings that match a pattern with some other string. In order to do that in Golang, we can either use the native functions that the strings package of Go's standard library provides us with or we can write the logic for the same on our own.
In this article, we will see different examples, where we will use the two most used functions of the strings package. These functions are −
strings.Replace()
strings.ReplaceAll()
Let's first consider the signature of these functions to know a little more about them.
Syntax of strings.Replace()
func Replace(s, old, new string, n int) string
The first parameter in the above function is the string that contains a substring that we want to match with another string for replacement.
The second parameter is the substring that we want to replace with the new string.
The third parameter is the string with which we want to replace the pattern that got matched in the string.
The last parameter is the number of occurrences we want to replace.
It should be noted that if we want to replace all the occurrences that got matched with the pattern, then we should pass -1 as the last argument when we call the Replace function.
Let's explore an example of the Replace function to understand how it works.
Example 1
Consider the code shown below.
package main import ( "fmt" "strings" ) func main() { var str string = "It is not is a string is" res := strings.Replace(str, "is", "isn't", 2) fmt.Println(res) }
Output
If we run the command go run main.go on the above code, then we will get the following output in the terminal.
It isn't not isn't a string is
Notice how we are just replacing the first two occurrences of that matched string. In case we want to replace all the occurrences, we can use the code shown below.
Example 2
Consider the code shown below.
package main import ( "fmt" "strings" ) func main() { var str string = "It is not is a string is" res := strings.Replace(str, "is", "isn't", -1) fmt.Println(res) }
Output
If we run the command go run main.go on the above code, then we will get the following output in the terminal.
It isn't not isn't a string isn't
The strings.ReplaceAll() function behaves similar to Replace() with -1 as the last argument.
Example 3
Consider the code shown below.
package main import ( "fmt" "strings" ) func main() { var str string = "It is not is a string is" res := strings.ReplaceAll(str, "is", "isn't") fmt.Println(res) }
Output
If we run the command go run main.go on the above code, then we will get the following output in the terminal.
It isn't not isn't a string isn't
- Related Articles
- How to Replace characters in a Golang string?
- How to Search and Replace text in Python?
- How to search and replace text in a file using Python?
- Search and Replace in Python
- How do I search and replace specific chars at the beginning of a string in MySQL?
- How to replace “and” in a string with “&” in R?
- Golang program to replace elements in a slice
- How to split a string in Golang?
- How to print a string in Golang?
- Golang program to replace the spaces of string with a specific character
- How to find and replace string in MySQL database for a particular string only?
- How to search for a string in JavaScript?
- C++ Program to find and replace in a string
- Recursive Search and Replace in Text Files in Linux
- Replace() vs ReplaceAll() in Golang
