Golang bytes.SplitAfter() function example
bytes package
SplitAfter slices the input slice into all subslices after each instance of separator(2nd parameter) and returns a slice of those subslices. If separator is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.
Golang bytes.SplitAfter() function usage example
package main
import (
"fmt"
"bytes"
)
func main() {
str := []byte("I LOVE THIS WORLD!")
for i, rune := range bytes.SplitAfter(str, []byte{'L'}) {
fmt.Printf("Counter %d : %s\n", i , string(rune))
}
fmt.Println("--------------")
for i, rune := range bytes.SplitAfter(str, []byte{'T'}) {
fmt.Printf("Counter %d : %s\n", i , string(rune))
}
fmt.Println("--------------")
for i, rune := range bytes.SplitAfter(str, []byte{}) {
fmt.Printf("Counter %d : %s\n", i , string(rune))
}
}
Output :
Counter 0 : I L
Counter 1 : OVE THIS WORL
Counter 2 : D!
Counter 0 : I LOVE T
Counter 1 : HIS WORLD!
Counter 0 : I
Counter 1 :
Counter 2 : L
Counter 3 : O
Counter 4 : V
Counter 5 : E
Counter 6 :
Counter 7 : T
Counter 8 : H
Counter 9 : I
Counter 10 : S
Counter 11 :
Counter 12 : W
Counter 13 : O
Counter 14 : R
Counter 15 : L
Counter 16 : D
Counter 17 : !
Advertisement
Something interesting
Tutorials
+5.2k Golang : Issue HTTP commands to server and port example
+7.8k Swift : Convert (cast) String to Double
+7.3k Golang : File system scanning
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+6.2k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+26.3k Golang : Calculate future date with time.Add() function
+19.6k Golang : Get current URL example
+6.2k Golang : Calculate US Dollar Index (DXY)
+8k Findstr command the Grep equivalent for Windows
+6.5k Golang : Combine slices of complex numbers and operation example
+16.3k Golang : Loop each day of the current month example