Skip to content

strings.Trim sometimes trims incorrectly #19371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asCii88 opened this issue Mar 2, 2017 · 10 comments
Closed

strings.Trim sometimes trims incorrectly #19371

asCii88 opened this issue Mar 2, 2017 · 10 comments

Comments

@asCii88
Copy link

asCii88 commented Mar 2, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8 windows/amd64

What operating system and processor architecture are you using (go env)?

GOARCH=amd64

What did you do?

I run this

aux := "PW/V2/C2/RQ/CMD/45465/44545"
fmt.Println(strings.Trim(aux, "PW/V2/C2/RQ/"))

Playground

What did you expect to see?

Output should be CMD/45465/44545

What did you see instead?

Output is MD/45465/44545

@cespare
Copy link
Contributor

cespare commented Mar 2, 2017

The second argument to Trim is a set of code points, not a prefix/suffix.

You probably want strings.TrimPrefix.

@cespare cespare closed this as completed Mar 2, 2017
@asCii88
Copy link
Author

asCii88 commented Mar 2, 2017

Thank you, sorry.

Best way to get an answer on the Internet is to be wrong :)

@ianlancetaylor
Copy link
Contributor

This mistake is practically a FAQ. Perhaps we can clarify the docs somehow.

@bradfitz
Copy link
Contributor

bradfitz commented Mar 2, 2017

The docs at least say "cutset" these days:

https://golang.org/pkg/strings/#Trim

And there's an example that says !!! Achtung! Achtung! !!! which is only more alarming when it's shouted at you verbally.

@davecheney
Copy link
Contributor

davecheney commented Mar 2, 2017 via email

@bradfitz
Copy link
Contributor

bradfitz commented Mar 2, 2017

Dave, totally agree. I was only saying it used to be even worse. :)

@davecheney
Copy link
Contributor

davecheney commented Mar 2, 2017 via email

@bradfitz
Copy link
Contributor

bradfitz commented Mar 2, 2017

🔪🔪🔪 stab away

@asCii88
Copy link
Author

asCii88 commented Mar 2, 2017

Yep, exactly. I Googled code points and I didn't even think about cutset.
I still have no idea what it does after seeing the example. I would assume it just cuts each time it finds the other string.
Then again, I was trying at first with TrimLeft, and didn't realize there was a TrimPrefix.

@zigo101
Copy link

zigo101 commented Mar 7, 2017

@golang golang locked and limited conversation to collaborators Mar 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants