To remove a character at a specific index in a string in Go, you can use string slicing.
In this example,
str1
with a value of "Hello World"
.package main
import "fmt"
func main() {
str1 := "Hello World"
indexToRemove := 6
str1 = str1[:indexToRemove] + str1[indexToRemove+1:]
fmt.Println("Modified string is:", str1)
}
Modified string is: Hello orld
In this example,
str2
with a value of "apple"
.package main
import "fmt"
func main() {
str2 := "apple"
indexToRemove := 2
str2 = str2[:indexToRemove] + str2[indexToRemove+1:]
fmt.Println("Modified string is:", str2)
}
Modified string is: aple
In this tutorial, we learned How to Remove Character at specific Index in a String in Go language with well detailed examples.