How to split byte array in Go

2 Answers

0 votes
package main 
   
import ( 
    "fmt"
    "bytes"
) 
   
func main() { 
    ba := []byte("go java c c++ php python")
   
   	r := bytes.SplitAfter(ba, []byte("p")) 
    
	fmt.Printf("%s\n", r);
	fmt.Printf("%s\n", r[0]);
	fmt.Printf("%s\n", r[1]);
	fmt.Printf("%s\n", r[2]);
	fmt.Printf("%s\n", r[3]);
}
 
 
    
/*
run:
    
[go java c c++ p hp  p ython]
go java c c++ p
hp
 p
ython
   
*/

 



answered Aug 6, 2020 by avibootz
0 votes
package main 
   
import ( 
    "fmt"
    "bytes"
) 
   
func main() { 
    ba := []byte("go java c c++ php python")
   
   	r := bytes.SplitAfter(ba, []byte("p")) 
    
	fmt.Printf("%s\n", r);
	
	for i := 0; i < len(r); i++ {
      	fmt.Printf("%s\n", r[i]);
    }
}
 
 
    
/*
run:
    
[go java c c++ p hp  p ython]
go java c c++ p
hp
 p
ython
   
*/

 



answered Aug 6, 2020 by avibootz
edited Aug 6, 2020 by avibootz

Related questions

1 answer 107 views
1 answer 97 views
3 answers 150 views
3 answers 148 views
148 views asked Mar 10, 2025 by avibootz
1 answer 132 views
132 views asked Feb 15, 2025 by avibootz
1 answer 103 views
1 answer 126 views
...