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.trim() is a bug????? #36315

Closed
niqinge opened this issue Dec 30, 2019 · 4 comments
Closed

strings.trim() is a bug????? #36315

niqinge opened this issue Dec 30, 2019 · 4 comments

Comments

@niqinge
Copy link

niqinge commented Dec 30, 2019

func strTrim () {
str := "wrong repayment time. "
resp := strings.Trim(str, "rpc error: code ")
fmt.Println(resp)
}

output: wrong repayment time.

BUT

func strTrim () {
str := "wrong repayment time. "
resp := strings.Trim(str, "rpc error: code = Unknown desc =")
fmt.Println(resp)
}

output:g repayment time.

What is it? why?????

@obeyda
Copy link
Contributor

obeyda commented Dec 30, 2019

https://play.golang.org/p/C_SuAmJkyH0
Let's work it out:
a) when trying to trim the left side withs="wrong repayment time. " and Cutset="rpc error: code ":

  1. the first character is a w, does Cutset contains a w? no, so return;

b) when trying to trim the left side withs="wrong repayment time. " and Cutset="rpc error: code = Unknown desc =":

  1. the 1st character is a w, does Cutset contains a w? yes, so continue;
  2. the 2nd character is a r, does Cutset contains a w? yes, so continue;
  3. the 3rd character is a o, does Cutset contains a w? yes, so continue;
  4. the 4th character is a n, does Cutset contains a w? yes, so continue;
  5. the 5th character is a g, does Cutset contains a w? no, so return;

this is not exactly how it's implemented, the Trim() func basically calls indexFunc to trim the left side,
by reading the source code you'll have a better understanding on how it's working and handling different ascii/non-ascii runes.

@malisetti
Copy link

malisetti commented Dec 30, 2019

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

The second argument to string.Trim is a set(cut set) not a single string.

@ALTree
Copy link
Member

ALTree commented Dec 30, 2019

As it was explained above, this is working as intended. You can use TrimSuffix for trimming whole substrings. Closing here, since this is not a bug and your question has been answered.

@ALTree ALTree closed this as completed Dec 30, 2019
@xibolun
Copy link

xibolun commented Apr 1, 2020

https://play.golang.org/p/C_SuAmJkyH0
Let's work it out:
a) when trying to trim the left side withs="wrong repayment time. " and Cutset="rpc error: code ":

  1. the first character is a w, does Cutset contains a w? no, so return;

b) when trying to trim the left side withs="wrong repayment time. " and Cutset="rpc error: code = Unknown desc =":

  1. the 1st character is a w, does Cutset contains a w? yes, so continue;
  2. the 2nd character is a r, does Cutset contains a w? yes, so continue;
  3. the 3rd character is a o, does Cutset contains a w? yes, so continue;
  4. the 4th character is a n, does Cutset contains a w? yes, so continue;
  5. the 5th character is a g, does Cutset contains a w? no, so return;

this is not exactly how it's implemented, the Trim() func basically calls indexFunc to trim the left side,
by reading the source code you'll have a better understanding on how it's working and handling different ascii/non-ascii runes.

is it should be

  1. the 1st character is a w, does Cutset contains a w? yes, so continue;
  2. the 2nd character is a r, does Cutset contains a r? yes, so continue;
  3. the 3rd character is a o, does Cutset contains a o? yes, so continue;
  4. the 4th character is a n, does Cutset contains a n? yes, so continue;
  5. the 5th character is a g, does Cutset contains a g? no, so return;

???

@golang golang locked and limited conversation to collaborators Apr 1, 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

6 participants