Home > Mobile >  convert string of array to array of string
convert string of array to array of string

Time:09-06

I have a string of array. for example

"['a','b','c']"

which i want to convert to array of string as below

["a","b","c"]

can someone help me on this?

CodePudding user response:

var s = "['a','b','c']"
ss := strings.Split(strings.Trim(s, "[]"), ",")
a := make([]string, len(ss))
for i := range ss {
    a[i] = strings.Trim(ss[i], "'")
}
out, err := json.Marshal(a)
if err != nil {
    panic(err)
}
fmt.Println(string(out))
// output: ["a","b","c"]

https://go.dev/play/p/kQ0Up06K9zz

  • Related