The difference between the TrimRight and Trimsuffix of the Golang strings package

Source: Internet
Author: User
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!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.