Trong bài này mình có đang cần xóa bớt các kí tự trong 1 string
Mình tìm được bài này:
https://socketloop.com/tutorials/golang-remove-characters-from-string-example
Lưu code lại lỡ trang web của họ bay màu
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