strings.Split in Go

Viewed 19839

The file names.txt consists of many names in the form of:

"KELLEE","JOSLYN","JASON","INGER","INDIRA","GLINDA","GLENNIS"

Does anyone know how to split the string so that it is individual names separated by commas?

KELLEE,JOSLYN,JASON,INGER,INDIRA,GLINDA,GLENNIS

The following code splits by comma and leaves quotes around the name, what is the escape character to split out the ". Can it be done in one Split statement, splitting out "," and leaving a comma to separate?

package main

import "fmt"
import "io/ioutil"
import "strings"

func main() {
        fData, err := ioutil.ReadFile("names.txt")  // read in the external file
    if err != nil {
        fmt.Println("Err is ", err)     // print any error
    }
    strbuffer := string(fData)  // convert read in file to a string

    arr := strings.Split(strbuffer, ",")

    fmt.Println(arr)

}

By the way, this is part of Project Euler problem # 22. http://projecteuler.net/problem=22

2 Answers
Related