This is a creation in Article, where the information may have evolved or changed.
Yesterday encountered a problem, thestrings.TrimRight("cyeamblog.go", ".go")result is actually"cyeambl", it makes me baffled. Of course, look at the official document explanation:
Func TrimRight (S string, cutset string) stringtrimright returns a slice of the string s, with all trailing Unicode code PO INTs contained in Cutset removed.
Func trimsuffix (s, suffix string) stringtrimsuffix returns s without the provided trailing suffix string. If s doesn ' t end with suffix, S is returned unchanged.
TrimSuffixCan see, this is what I want to use, in line with my requirements. But ITrimRightdon't know what the difference is. Apart from the code, the results are not as clear as others. Go to the big Google today to search, see this title is this a bug? Strings. TrimRight seems to being cutting too much, I'm happy to die. I think so too, just embarrassed to go up and ask.
Look at this sort of figure out. Presumably the explanation is thatTrimRightall the characters in the second argument string are taken out and removed as long as they are equal to any one of them.cutsetthat is, the parameter is removed as a combination of all character permutations.
Attach an example:
package main
import (
"log"
"strings"
)
func main() {
log.Println(strings.TrimRight("abba", "ba"))
log.Println(strings.TrimRight("abcdaaaaa", "abcd"))
log.Println(strings.TrimSuffix("abcddcba", "dcba"))
}
Results:
2015/08/13 15:56:30
2015/08/13 15:56:30
2015/08/13 15:56:30 abcd
Please refer to the complete source code in this article.
###### Reference 1. Is this a bug? Strings. TrimRight seems to be cutting too much. -Golang-nuts
Original link: Golang strings package of trimright and Trimsuffix differences, reproduced please indicate the source!