Does anyone know how to split a string in Golang by length?
For example to split "helloworld" after every 3 characters, so it should ideally return an array of "hel" "low" "orl" "d"?
Alternatively a possible solution would be to also append a newline after every 3 characters..
All ideas are greatly appreciated!
An easy solution using regex
re := regexp.MustCompile(
(\S{3})
) x := re.FindAllStringSubmatch("HelloWorld", -1) fmt.Println(x)https://play.golang.org/p/mfmaQlSRkHe
Make sure to convert your string into a slice of rune: see "Slice string into letters".
for
automatically convertsstring
torune
so there is no additional code needed in this case to convert thestring
torune
first.r[n:n+3]
will work best with a being a slice of rune.The index will increase by one every rune, while it might increase by more than one for every byte in a slice of string: "世界":
i
would be 0 and 3: a character (rune) can be formed of multiple bytes.For instance, consider
s := "世a界世bcd界efg世"
: 12 runes. (see play.golang.org)If you try to parse it byte by byte, you will miss (in a naive split every 3 chars implementation) some of the "index modulo 3" (equals to 2, 5, 8 and 11), because the index will increase past those values:
The output:
But if you were to iterate on runes (
a := []rune(s)
), you would get what you expect, as the index would increase one rune at a time, making it easy to aggregate exactly 3 characters:Output:
Here is another example (you can try it here):
I tried 3 version to implement the function, the function named "splitByWidthMake" is fastest.
These functions ignore the unicode but only the ascii code.
Also needed a function to do this recently, see example usage here
Here is another variant playground. It is by far more efficient in terms of both speed and memory than other answers. If you want to run benchmarks here they are benchmarks.