Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove quotes from around a string in Golang

Tags:

go

I have a string in Golang that is surrounded by quote marks. My goal is to remove all quote marks on the sides, but to ignore all quote marks in the interior of the string. How should I go about doing this? My instinct tells me to use a RemoveAt function like in C#, but I don't see anything like that in Go.

For instance:

"hello""world"

should be converted to:

hello""world

For further clarification, this:

"""hello"""

would become this:

""hello""

because the outer ones should be removed ONLY.

like image 970
RamaRaunt Avatar asked May 27 '17 23:05

RamaRaunt


People also ask

How do I remove single quotes from a string in Golang?

In your go source files, if you try to use single quotes for a multi-character string literal, you'll get a compiler error similar to illegal rune literal . What you can do instead for removing quotes from the start and end of a string, is use the strings. Trim function to take care of it.

How do you remove quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.


5 Answers

Use a slice expression:

s = s[1 : len(s)-1]

If there's a possibility that the quotes are not present, then use this:

if len(s) > 0 && s[0] == '"' {
    s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
    s = s[:len(s)-1]
}

playground example

like image 174
Bayta Darell Avatar answered Oct 18 '22 20:10

Bayta Darell


Use a slice expression:

s = s[1 : len(s)-1]

If there's a possibility that the quotes are not present, then use this:

if len(s) > 0 && s[0] == '"' {
    s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
    s = s[:len(s)-1]
}

playground example

like image 35
Bayta Darell Avatar answered Sep 18 '22 22:09

Bayta Darell


strings.Trim() can be used to remove the leading and trailing whitespace from a string. It won't work if the double quotes are in between the string.

// strings.Trim() will remove all the occurrences from the left and right

s := `"""hello"""`
fmt.Println("Before Trim: " + s)                    // Before Trim: """hello"""
fmt.Println("After Trim: " + strings.Trim(s, "\"")) // After Trim: hello

// strings.Trim() will not remove any occurrences from inside the actual string

s2 := `""Hello" " " "World""`
fmt.Println("\nBefore Trim: " + s2)                  // Before Trim: ""Hello" " " "World""
fmt.Println("After Trim: " + strings.Trim(s2, "\"")) // After Trim: Hello" " " "World

Playground link - https://go.dev/play/p/yLdrWH-1jCE

like image 30
Rahul Satal Avatar answered Sep 17 '22 22:09

Rahul Satal


strings.Trim() can be used to remove the leading and trailing whitespace from a string. It won't work if the double quotes are in between the string.

// strings.Trim() will remove all the occurrences from the left and right

s := `"""hello"""`
fmt.Println("Before Trim: " + s)                    // Before Trim: """hello"""
fmt.Println("After Trim: " + strings.Trim(s, "\"")) // After Trim: hello

// strings.Trim() will not remove any occurrences from inside the actual string

s2 := `""Hello" " " "World""`
fmt.Println("\nBefore Trim: " + s2)                  // Before Trim: ""Hello" " " "World""
fmt.Println("After Trim: " + strings.Trim(s2, "\"")) // After Trim: Hello" " " "World

Playground link - https://go.dev/play/p/yLdrWH-1jCE

like image 28
Rahul Satal Avatar answered Oct 18 '22 21:10

Rahul Satal


Use slice expressions. You should write robust code that provides correct output for imperfect input. For example,

package main

import "fmt"

func trimQuotes(s string) string {
    if len(s) >= 2 {
        if s[0] == '"' && s[len(s)-1] == '"' {
            return s[1 : len(s)-1]
        }
    }
    return s
}

func main() {
    tests := []string{
        `"hello""world"`,
        `"""hello"""`,
        `"`,
        `""`,
        `"""`,
        `goodbye"`,
        `"goodbye"`,
        `goodbye"`,
        `good"bye`,
    }

    for _, test := range tests {
        fmt.Printf("`%s` -> `%s`\n", test, trimQuotes(test))
    }
}

Output:

`"hello""world"` -> `hello""world`
`"""hello"""` -> `""hello""`
`"` -> `"`
`""` -> ``
`"""` -> `"`
`goodbye"` -> `goodbye"`
`"goodbye"` -> `goodbye`
`goodbye"` -> `goodbye"`
`good"bye` -> `good"bye`
like image 3
peterSO Avatar answered Oct 18 '22 19:10

peterSO