I have a string that may or may not be surrounded with double quotes or single quotes. I want to remove the first set of quotes, if present. So:
"foo" --> foo
""foo"" --> "foo"
'foo' --> foo
'foo --> foo
Etc.
I found this answer, which describes using a slice expression to index into the first and last characters of the string, then grab a slice of the string if either character is a quote. So I tried to tweak the code to cover both single and double quotes:
func stripQuotes(str string) string {
s := str[:]
if len(s) > 0 && (s[0] == '"' || s[0] == "'") {
s = s[1:]
}
if len(s) > 0 && (s[len(s)-1] == '"' || s[len(s)-1] == "'") {
s = s[:len(s)-1]
}
return s
}
But it returns an error:
cannot convert "'" (type untyped string) to type byte
So I tried converting the single quote to a byte, but that didn't work either:
...
if len(s) > 0 && (s[0] == '"' || s[0] == byte("'")) {
...
It returns the error:
cannot convert "'" (type untyped string) to type byte
I know that there's some basic string handling knowledge I'm missing here, but I'm not sure what. Is there a simple way to identify single quotes in a string?