Skip to content
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

strings.TrimRight unexpected output #41557

Closed
lafh opened this issue Sep 22, 2020 · 2 comments
Closed

strings.TrimRight unexpected output #41557

lafh opened this issue Sep 22, 2020 · 2 comments

Comments

@lafh
Copy link

@lafh lafh commented Sep 22, 2020

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

$ go version
go version go1.15 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env

What did you do?

https://play.golang.org/p/N87az0K_2by

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.TrimRight("abc@xx.com", "@xx.com"))

}

What did you expect to see?

abc

What did you see instead?

ab

@ALTree
Copy link
Member

@ALTree ALTree commented Sep 22, 2020

In TrimRight, the second parameter is a cutset: TrimRight(s string, cutset string), i.e. a set of individual characters to be trimmed.

Passing "@xx.com" doesn't mean "trim right at @xx.com", it means "trim all @ x, c, o, and m characters from the right". That's why your c gets trimmed, too.

Working as intended, closing this.

@ALTree ALTree closed this Sep 22, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Sep 22, 2020

By the way, you might be looking for strings.TrimSuffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.