Skip to content

Something wrong with strings.TrimLeft when string start with '/v' #35149

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
zhaozy93 opened this issue Oct 25, 2019 · 4 comments
Closed

Something wrong with strings.TrimLeft when string start with '/v' #35149

zhaozy93 opened this issue Oct 25, 2019 · 4 comments

Comments

@zhaozy93
Copy link

zhaozy93 commented Oct 25, 2019

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

$ go version
go1.13.2

Does this issue reproduce with the latest release?

Not Sure

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

go env Output
$ go env

What did you do?

Usually we use strings.TrimLeft to trim some fixed prefix, however when exec strings.TrimLeft("./csv/visitor.csv", "./csv/"), the EXPECTED result is "vistor.csv", the REAL result is "istor.csv".

It's amazing.

https://play.golang.com/p/EeY2DKiWeRR

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println("----------a-----------")
	a := "./csv/visitor.csv"
	fmt.Println(a)
	fff := strings.TrimLeft(a, "./csv/")
	fmt.Println(fff)

	fmt.Println("----------b-----------")
	b := "./csv/visitor.csv"
	fmt.Println(b)
	fff = strings.TrimLeft(b, "./csv")
	fmt.Println(fff)
	
	fmt.Println("----------c-----------")
	c := "./csv/visitor.csv"
	fmt.Println(c)
	fff = strings.TrimLeft(b, "./")
	fmt.Println(fff)
}

What did you expect to see?

----------a-----------
./csv/visitor.csv
visitor.csv
----------b-----------
./csv/visitor.csv
visitor.csv
----------c-----------
./csv/visitor.csv
csv/visitor.csv

What did you see instead?

----------a-----------
./csv/visitor.csv
isitor.csv
----------b-----------
./csv/visitor.csv
isitor.csv
----------c-----------
./csv/visitor.csv
csv/visitor.csv

@zhaozy93
Copy link
Author

I think you want https://godoc.org/strings#TrimPrefix instead.

@zhaozy93
Copy link
Author

Anyone could close this if you want.

@tmthrgd
Copy link
Contributor

tmthrgd commented Oct 25, 2019

Check the documentation: https://golang.org/pkg/strings/#TrimLeft:

TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.

To remove a prefix, use TrimPrefix instead.

The TrimLeft function is removing every character from the cutest from the start of the string whereas what you’re looking is what TrimPrefix does, which is to remove a fixed prefix from the string.

@zhaozy93
Copy link
Author

Check the documentation: https://golang.org/pkg/strings/#TrimLeft:

TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
To remove a prefix, use TrimPrefix instead.

The TrimLeft function is removing every character from the cutest from the start of the string whereas what you’re looking is what TrimPrefix does, which is to remove a fixed prefix from the string.

yeah I have found this. stupid -_-
Thanks

@golang golang locked and limited conversation to collaborators Oct 24, 2020
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

3 participants