Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Replace a character with another in a string in Go (Golang)

Posted on March 16, 2023March 16, 2023 by admin

Table of Contents

  • Overview
  • Code:

Overview

In GO string are UTF-8 encoded. strings package of GO provides a Replace method that can be used to replace a character with another in a given string. It returns a copy of the string.

Below is the signature of the function.

  • Supplies the character that is to be replaced as old
  • Supply the character you want to be replaced with as new
  • n represents the number of replacements. If n is -1 then all instances of old are replaced with new
func Replace(s, old, new string, n int)

Let’s look at the working code

Code:

package main
import (
"fmt"
"strings"
)
func main() {
//It will replaces 1 instance of a with 1
res := strings.Replace("abcdabxyabr", "a", "1", 1)
fmt.Println(res)
//It will replace all instances of a with 1
res = strings.Replace("abcdabxyabr", "a", "1", -1)
fmt.Println(res)
}

Output:

1bcdabxyabr
1bcd1bxy1br

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP