Golang : Remove characters from string example
Problem:
You have a string and you want to remove certain characters from the string base on a set of characters. You've tried using strings.Replace()
function to remove the unwanted characters, but you found out that it removes one character at a time, not a set of characters. To remove a set of characters with strings.Replace()
function requires looping and can be inefficient. Is there a better way to remove a set of characters from a string?
Solution:
Combine strings.Map()
and strings.IndexRune()
functions to remove a set of unwanted characters from the string.
Here you go!
package main
import (
"fmt"
"strings"
)
func removeCharacters(input string, characters string) string {
filter := func(r rune) rune {
if strings.IndexRune(characters, r) < 0 {
return r
}
return -1
}
return strings.Map(filter, input)
}
func main() {
str := "你好吗? 我好! 好我好!? 你好好!"
stripped := strings.Replace(str, "我好", "", -1) // Replace will work with a single unicode
fmt.Println("Before : ", str)
fmt.Println("After : ", stripped)
str = "Happy Go Lucky!"
stripped = strings.Replace(str, "a", "", -1) // only works with a single character
fmt.Println("Before : ", str)
fmt.Println("After : ", stripped)
str = "Happy Go Lucky!"
stripped = strings.Replace(str, "aGo", "", -1) // but NOT with a set of characters
fmt.Println("Before : ", str)
fmt.Println("After : ", stripped)
fmt.Println("-------------------------------------------------")
str = "你好吗? 我好! 好我好!? 你好好!"
stripped = removeCharacters(str, "我好") // now with remove/strip a set of unicode characters
fmt.Println("Before : ", str)
fmt.Println("After : ", stripped)
str = "Happy Go Lucky!"
stripped = removeCharacters(str, "aGo") // will work with a set of characters
fmt.Println("Before : ", str)
fmt.Println("After : ", stripped)
}
Output:
Before : 你好吗? 我好! 好我好!? 你好好!
After : 你好吗? ! 好!? 你好好!
Before : Happy Go Lucky!
After : Hppy Go Lucky!
Before : Happy Go Lucky!
After : Happy Go Lucky!
Before : 你好吗? 我好! 好我好!? 你好好!
After : 你吗? ! !? 你!
Before : Happy Go Lucky!
After : Hppy Lucky!
References:
https://golang.org/pkg/strings/#IndexRune
https://golang.org/pkg/strings/#Map
https://www.socketloop.com/tutorials/golang-remove-dashes-or-any-character-from-string
See also : Find and replace a character in a string in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.7k Golang : Check if password length meet the requirement
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+11.6k Golang : Transform comma separated string to slice example
+10.8k Golang : Simple client-server HMAC authentication without SSL example
+7.1k Golang : HTTP Server Example
+15.8k Golang : XML to JSON example
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+13.3k Golang : How to filter a map's elements for faster lookup
+8.2k Mac OSX : Get a process/daemon status information
+23.1k Golang : Time slice or date sort and reverse sort example
+46.2k Golang : How to convert JSON string to map and slice
+7.1k Prevent Write failed: Broken pipe problem during ssh session with screen command