Golang program to replace a character at a specific index


In this tutorial, we will inculcate how to replace a character at a specific index using few examples. The output will be printed on the console using fmt.Println() function. Let’s dive deep into the examples and see how it’s done.

Method 1: Using Replace Function

In this method, we will learn how to replace a character at a specific index using replace function. The output is printed on console using fmt.Println() function. Let’s understand this through the code.

Syntax

func Replace(str, oldstr, newstr string, m int) string

This function is used to return the copy of the string which is being replaced by new one. The str here is the original string, oldstr is the string that we want to replace, newstr is the string which replaces the old one and m is the number of times the old is being replaced.

Algorithm

Step 1 − Create a package main and import fmt package and strings in the program.

Step 2 − Create a string str in the program whose content is to be replaced by the given characters.

Step 3 − Using replace function replace the old strings characters with new some new characters.

Step 4 − Print the new string on the console using fmt.Println() function where ln means new line.

Example

Golang program to replace a character at a specific index using replace function

package main
import (
   "fmt"
   "strings"
)
func main() {

   str := "Hi, i am an engineer"
   
   fmt.Println("This is the original string:")
   fmt.Println(str)
   
   result := strings.Replace(str, "e", "E", 3)
   
   fmt.Println("\nString after replacement is:")
   fmt.Println("Result:", result)
   
}

Output

This is the original string:
Hi, i am an engineer

String after replacement is:
Result: Hi, i am an EnginEEr

Method 2: Using ReplaceAll Function

In this method, we will learn how to replace a character at a specific index using ReplaceAll function. The output is printed on console using fmt.Println() function. Let’s understand this through the code.

Syntax

func ReplaceAll(str, oldstr, newstr string) string

This function is used to replace all the old string with a new string. Here, str is the original string, oldstr is the string that will be replaced and newstr is the string that replaces the oldstr.

Algorithm

Step 1 − Create a package main and import fmt package and strings in the program.

Step 2 − Create a main function and further create string str in the program whose content is to be replaced by the given characters.

Step 3 − Using ReplaceAll function replace the old string with the new string that will be printed.

Step 4 − Print the new string on the console using fmt.Println() function where ln means new line.

Example

Golang program to replace a character at a specific index using ReplaceAll function

package main
import (
   "fmt"
   "strings"
)
func main() {

   str := "Hi, I am an engineer"
   
   fmt.Println("The original string is:")
   fmt.Println("String:", str)
   
   //Using ReplaceAll() function
   result := strings.ReplaceAll(str, "engineer", "developer")
   
   // Displaying the result
   fmt.Println("\nString after replacement is:")
   fmt.Println(result)
}

Output

The original string is:
String: Hi, I am an engineer
String after replacement is:
Hi, I am an developer

Conclusion

This program of replacing the character at a specific index is executed using two examples. In the first example replace function is used and in the second example ReplaceAll function is used. Hence, the program executed successfully.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements