0

I am trying to take an array and split it into an array of strings, this is my code:

if split != "" {

    for i := 0; i < len(split); i++ {

        for j := 0; j < len(split); j += 0 {

            splits := []byte(split)

            if splits[i] == ' ' {


                result := split[i] - split[j]

                for k := 0; k <= i; k++ {

                    fitting := make([]byte, result)
                    fitting[k] = splits[k]
                    fmt.Println(fitting[k])

                    if k > i-1 {

                        fittings := string(fitting[:])
                        word := []string{}
                        words := append(word, fittings)
                        fmt.Println(split, words)
                    }
                }

            }
        }
    }
}
return Strings(split)

and this is my test case:

fmt.Println(actual, expected)

for i := 0; i < len(expected); i++ {

    if actual[i] != expected[i] {
        t.Errorf("does not match")
        t.Fail()
    }

}
}

None of it is really working.

4
  • 4
    What is your actual problem? You have posted your code and your test, okay. What kind of help are you looking for? PS: it also would be ideal if you provided minimal yet complete examples which are possible to run on play.golang.org with little to no modifications. Commented Sep 2, 2018 at 22:47
  • 2
    Please provide a short, self contained, correct example. Furthermore, please specify the structure of your inputs (including an example, preferably) and the expected outputs. It is unclear to me from your question precisely what you are trying to accomplish. Commented Sep 2, 2018 at 22:49
  • 1
    I just need to know how I could possibly take a string such as "hi li le" and make it into an array of strings such as ["hi","li","le"] Commented Sep 2, 2018 at 22:56
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Sep 3, 2018 at 7:55

2 Answers 2

7

I just need to know how I could possibly take a string such as "hi li le" and make it into an array of strings such as ["hi","li","le"]

Yes with strings.Split or strings.Fields.

for _, word := range strings.Fields("hi li le") {
    fmt.Println(word)
}

Here's a way to do it manually, for illustration.

func split(tosplit string, sep rune) []string {
    var fields []string

    last := 0
    for i,c := range tosplit {
        if c == sep {
            // Found the separator, append a slice
            fields = append(fields, string(tosplit[last:i]))
            last = i + 1
        } 
    }

    // Don't forget the last field
    fields = append(fields, string(tosplit[last:]))

    return fields
}

func main() {
    str := "hello world stuff"
    for _,field := range split(str, ' ') {
        fmt.Println(field)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I just need to know how I could possibly take a string such as "hi li le" and make it into an array of strings such as ["hi","li","le"]

I would like to respond this with simple a simple example:

s := "hi li le"
arr := strings.Split(s, " ")
fmt.Println(arr)

Note: Make sure to import strings & fmt in your package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.