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 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 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 as completed Sep 22, 2020
@ianlancetaylor
Copy link
Contributor

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

@golang golang locked and limited conversation to collaborators Sep 22, 2021
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

4 participants