Golang bytes.IndexAny() function example

package bytes

IndexAny interprets the input slice as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the first occurrence in the input slice of any of the Unicode code points search key. It returns -1 if chars is empty or if there is no code point(search key) in common.

Golang bytes.IndexAny() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

 search_input := []byte("abcd和efghi")

 indexany := bytes.IndexAny(search_input, "和") // if 和 is not available, indexany value will be -1

 fmt.Printf("Print everything after index number : %d [%s]\n", indexany, string(search_input[indexany:]))

 }

Output :

Print everything after index number : 4 [和efghi]

Advertisement