如何从字符串中删除双引号

I have a string that I'm pulling from a website, however, it has double quotes surrounding it and I'm trying to remove them. The string is for example:

" 54%"

I've tried:

strings.Trim(s[0], "\"")

But it's not removing the quotes.

Any help is much appreciated.

s := "\"  hello\""

fmt.Println("Without trim: " + s) 
// Without trim: "  hello"

fmt.Println("Trim double quotes: " + strings.Trim(s, "\"")) 
// Trim double quotes:   hello

See https://play.golang.org/p/0I2BtS6b_mO

Please give working code. Things that could be wrong:

  • You're using s[0] that means the first byte of s, did you mean to use s or is this an array of strings?
  • Your string may have trailing or leading whitespace
  • Your string may use smart quotes

Your code should work fine, perhaps you are misinterpreting some printed results or storing the wrong value?

ss := []string{"foo", `""`, `"bar"`, `"        54%"`}
for _, s := range ss {
  t := strings.Trim(s, `"`)
  fmt.Printf("OK:    [%s] --> [%s]
", s, t)
}
// OK:    [foo] --> [foo]
// OK:    [""] --> []
// OK:    ["bar"] --> [bar]
// OK:    ["        54%"] --> [        54%]
//         │           │       │         │
// quotes ─┴───────────┘       │         │
// no quotes ──────────────────┴─────────┘