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() has bug when trim some strings #14657

Closed
awkr opened this issue Mar 5, 2016 · 3 comments
Closed

strings.TrimRight() has bug when trim some strings #14657

awkr opened this issue Mar 5, 2016 · 3 comments

Comments

@awkr
Copy link

@awkr awkr commented Mar 5, 2016

Please answer these questions before submitting your issue. Thanks!

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

go1.6 darwin/amd64

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

darwin/amd64

  1. What did you do?
    (Use play.golang.org to provide a runnable example, if possible.)

package main

import (
"fmt"
"path/filepath"
"strings"
)

func main() {
path := "Order.thrift"

b := filepath.Base(path)
fmt.Println(b) // Order.thrift

e := filepath.Ext(path)
fmt.Println(e) // .thrift

n := strings.TrimRight(b, e)
fmt.Println(n)

}

  1. What did you expect to see?

Order

  1. What did you see instead?

Orde

@davecheney
Copy link
Contributor

@davecheney davecheney commented Mar 5, 2016

Trimright removes characters in the set e from b, starting from the right. It's not what you want.

You want, n := b[:len(b)-len(e)]

As you know that e is the suffix of b

On 5 Mar 2016, at 20:15, Hongjian Zhu notifications@github.com wrote:

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?
go1.6 darwin/amd64

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

What did you do? (Use play.golang.org to provide a runnable example, if possible.)
package main

import (
"fmt"
"path/filepath"
"strings"
)

func main() {
path := "Order.thrift"

b := filepath.Base(path)
fmt.Println(b) // Order.thrift

e := filepath.Ext(path)
fmt.Println(e) // .thrift

n := strings.TrimRight(b, e)
fmt.Println(n)
}

What did you expect to see?
Order

What did you see instead?
Orde


Reply to this email directly or view it on GitHub.

@minux minux closed this Mar 5, 2016
@minux
Copy link
Member

@minux minux commented Mar 5, 2016

@awkr
Copy link
Author

@awkr awkr commented Mar 5, 2016

@davecheney, @minux thank you guys for your kindly replay :-]

@golang golang locked and limited conversation to collaborators Mar 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants