
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - String Replacement
The strings package of Golang has a Replace() function which we can use to replace some characters in a string with a new value. It replaces only a specified "n" occurrences of the substring.
In addition to Replace(), there is a ReplaceAll() function that replaces all the occurrences of a given substring with a new value.
Syntax
func Replace(s, old, new string, n int) string
Where,
s is the given string
old is the string which we want to replace
new is the string which will replace the old string
n represents the number of characters which we want to replace in the given string.
String Replacement Example 1
The following example demonstrates how you can use the Replace() function to replace a substring with a new string.
package main import ( "fmt" "strings" ) func main() { // Initializing the Strings s := "Go Programming Language" old := "Go" newstring := "Golang" n := 1 // Display the Strings fmt.Println("Original String: ", s) // Using the Replace Function testresult := strings.Replace(s, old, newstring, n) // Display the Replace Output fmt.Println("Replace Go with Golang:", testresult) }
Output
It will generate the following output −
Original String: Go Programming Language Replace Go with Golang: Golang Programming Language
String Replacement Example 2
In this example, we are going to see how to replace a single character with a new value at a particular defined postion or index.
package main import ( "fmt" "strings" ) func main() { // Initializing the Strings x := "Replace String Function" y := "Go Language" // Display the Strings fmt.Println("1st String:", x) fmt.Println("2nd String:", y) // Using the Replace Function test1 := strings.Replace(x, "i", "I", 2) test2 := strings.Replace(y, "g", "G", -1) // Display the Replace Output fmt.Println("\n Replace 'i' with 'I' in the 1st String: \n", test1) fmt.Println("\n Replace 'g' with 'G' in the 2nd String: \n", test2) }
Output
It will generate the following output −
1st String: Replace String Function 2nd String: Go Language Replace 'i' with 'I' in the 1st String: Replace StrIng FunctIon Replace 'g' with 'G' in the 2nd String: Go LanGuaGe