Home > Mobile >  Split the text and get an array of strings by space and get an array of strings if the text is longe
Split the text and get an array of strings by space and get an array of strings if the text is longe

Time:12-20

I need to split a text that is more than 500 characters long into several arrays. The task in full sounds like this:

The message is divided into blocks of 500 characters. If the message is more than 500 characters, then starting from 500 characters look for a space, if a space is found, then divide the message into parts in this place. If no space is found, then we divide the message by 500 characters.

My decision, this is just the beginning. We split the text with a regular expression, then go through a loop and add the data to the string if the length allows. But I think I'm confused, how do I get an array of strings, so that the length of each string is appropriate up to 500 characters?

re := regexp.MustCompile(`\s `)
res := re.Split(str, -1)

size := 500

finalString := ""
for i, _ := range res {
    if len(finalString " " res[i]) <= size {
        if len(finalString) == 0 {
            finalString  = res[i]
        }
        finalString  = " "   res[i]
    } else {
        break // can be added to a new line, and if the length is longer, I do not know what to do
    }
}

CodePudding user response:

Just to make sure I understand this correctly, you're looking to split text at the first space after every 500 characters?

Keep in mind that string concatenation can be relatively expensive (adding two chars to the end of your string like finalString = " " res[I] incurs O(N) complexity, since the string's underlying byte array must be copied.

A better solution is to rely on bytes.Buffer. I wrote this sample code up (note you can change splitLength — I didn't want to paste a 500 character input in here).

package main

import (
    "bytes"
    "fmt"
)

func main() {
    prettyPrint(split([]byte("1234567 123 12345 1234567"), 5))
}

func prettyPrint(b [][]byte) {
    for _, a := range b {
        fmt.Println(string(a))
    }
}

func split(b []byte, splitLength int) [][]byte {
    current := new(bytes.Buffer)
    var bSlice [][]byte
    counter := 0
    shouldTerminate := false

    for i, c := range b {
        if shouldTerminate == true && c == byte(32) {
            counter = 0
            shouldTerminate = false

            bSlice = append(bSlice, current.Bytes())
            current = new(bytes.Buffer)

            continue
        }

        counter  
        current.Write([]byte{c})

        if counter > splitLength {
            shouldTerminate = true
        }

        if i == len(b)-1 {
            bSlice = append(bSlice, current.Bytes())
        }
    }

    return bSlice
}

CodePudding user response:

Agree with the comments by @lickety-split, you need a strtok function implementation in go. You can use this implementation - https://github.com/dannav/tokenize or implement your own. Hope this helps!

  • Related