strings.TrimLeft removes extra char

861 views
Skip to first unread message

Archos

unread,
Oct 30, 2011, 8:07:01 PM10/30/11
to golang-nuts
It looks that strings.TrimLeft has a bug since it is removing an extra
character

===
package main

import (
"fmt"
"strings"
)

func main() {
file := "/var/tmp/servoconf/"
fmt.Println(strings.TrimLeft(file, "/var/tmp"))
}
===
The ouput is `servoconf/` but it's expected `/servoconf/`

David Symonds

unread,
Oct 30, 2011, 8:09:33 PM10/30/11
to Archos, golang-nuts
You misunderstand how strings.TrimLeft works.
http://golang.org/pkg/strings/#TrimLeft

As a hint, if you used strings.TrimLeft(file, "/amprtv") then your
program would work the same.


Dave.

Archos

unread,
Oct 30, 2011, 8:22:45 PM10/30/11
to golang-nuts
Oh my god! A lot of hours without sleep, it's time of coffe.
Thanks

jar...@enigma.io

unread,
Nov 25, 2013, 3:58:31 PM11/25/13
to golan...@googlegroups.com
Here is an example to illustrate the difference.

http://play.golang.org/p/7GUhO3rV77

a:= "t-est.ing this function"
fmt.Println(strings.TrimLeft(a,"tset. -hing"))
fmt.Println(strings.TrimPrefix(a,"test")
------------
function
.ing this function

chris dollin

unread,
Nov 26, 2013, 8:27:40 AM11/26/13
to jar...@enigma.io, golang-nuts
On 25 November 2013 20:58, <jar...@enigma.io> wrote
H

On Sunday, October 30, 2011 8:07:01 PM UTC-4, Archos wrote:
It looks that strings.TrimLeft has a bug since it is removing an extra
character

===
package main

import (
        "fmt"
        "strings"
)

func main() {
        file := "/var/tmp/servoconf/"
        fmt.Println(strings.TrimLeft(file, "/var/tmp"))
}
===
The ouput is `servoconf/` but it's expected `/servoconf/`

Note that the second argument to TrimLeft represents a /set/ of
characters, not a /sequence/. / is in the set of characters to remove.

Chris


--
Chris "allusive" Dollin

Svip

unread,
Nov 26, 2013, 8:30:13 AM11/26/13
to chris dollin, jar...@enigma.io, golang-nuts
It's also worth noticing that the original post was from October 2011.

chris dollin

unread,
Nov 26, 2013, 8:35:02 AM11/26/13
to Svip, jar...@enigma.io, golang-nuts
On 26 November 2013 13:30, Svip <svi...@gmail.com> wrote:

It's also worth noticing that the original post was from October 2011.

*embarrassed face*
Reply all
Reply to author
Forward
0 new messages