Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Golang | Splitting a slice of bytes after the specified separator
Next article icon

Golang | Splitting a slice of bytes after the specified separator

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to split the slice after the specified separator using a SplitN() function. This function splits a slice into all subslices after each instance of the given separator and returns a slice of the subslices between those separators. If the given separator is empty, then it splits after each UTF-8 sequence and the count indicates the number of subslices to return. It is defined under the bytes package so, you have to import bytes package in your program for accessing SplitN function. Syntax:
func SplitN(o_slice, sep []byte, m int) [][]byte
Here, o_slice is the original string, sep is the separator, and m is used to find the number of substrings to return. Here, if m>0, then it returns at most m subslices and the last string subslice will not split. If m == 0, then it will return nil. If m<0, then it will return all subslices. Example 1: C
// Go program to illustrate the concept // of splitting a slice of bytes package main  import (     "bytes"     "fmt" )  func main() {      // Creating and initializing     // the slice of bytes     // Using shorthand declaration     slice_1 := []byte{'!', '!', 'G', 'e', 'e', 'k', 's', 'f',              'o', 'r', 'G', 'e', 'e', 'k', 's', 'G', 'e',                                  'e', 'k', 's', '#', '#'}      slice_2 := []byte{'A', 'p', 'p', 'p', 'p', 'l', 'e'}      slice_3 := []byte{'%', 'g', '%', 'e', '%',             'e', '%', 'k', '%', 's', '%'}      // Displaying slices     fmt.Println("Original Slice:")     fmt.Printf("Slice 1: %s", slice_1)     fmt.Printf("\nSlice 2: %s", slice_2)     fmt.Printf("\nSlice 3: %s", slice_3)      // Splitting the slice of bytes     // Using SplitN function     res1 := bytes.SplitN(slice_1, []byte("eek"), 2)     res2 := bytes.SplitN(slice_2, []byte(""), 3)     res3 := bytes.SplitN(slice_3, []byte("%"), 0)      // Display the results     fmt.Printf("\n\nAfter splitting:\n")     fmt.Printf("\nSlice 1: %s", res1)     fmt.Printf("\nSlice 2: %s", res2)     fmt.Printf("\nSlice 3: %s", res3)  } 
Output:
  Original Slice:  Slice 1: !!GeeksforGeeksGeeks##  Slice 2: Apppple  Slice 3: %g%e%e%k%s%    After splitting:    Slice 1: [!!G sforGeeksGeeks##]  Slice 2: [A p ppple]  Slice 3: []  
Example 2: C
// Go program to illustrate the concept // of splitting a slice of bytes package main  import (     "bytes"     "fmt" )  func main() {      // Creating and Splitting     // the slice of bytes     // Using SplitN function     res1 := bytes.SplitN([]byte("****Welcome, to, GeeksforGeeks****"),         []byte(","), -1)      res2 := bytes.SplitN([]byte("Learning x how x to x"+            " trim x a x slice of bytes"),[]byte("x"), 3)      res3 := bytes.SplitN([]byte("Geeks,for,Geeks, Geek"), []byte(","), 0)      res4 := bytes.SplitN([]byte(""), []byte(","), 2)      // Display the results     fmt.Printf("\nFinal Result after splitting:\n")     fmt.Printf("\nSlice 1: %s", res1)     fmt.Printf("\nSlice 2: %s", res2)     fmt.Printf("\nSlice 3: %s", res3)     fmt.Printf("\nSlice 4: %s", res4) } 
Output:
  Final Result after splitting:    Slice 1: [****Welcome  to  GeeksforGeeks****]  Slice 2: [Learning   how   to x trim x a x slice of bytes]  Slice 3: []  Slice 4: []  

Next Article
Golang | Splitting a slice of bytes after the specified separator

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-Slices

Similar Reads

    Golang | Splitting a Slice Separated by the Specified Expression
    A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. I
    3 min read
    Golang | Splitting the string after the specified separator
    In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In the Go strings, you are allowed to split the string after the specified se
    3 min read
    How to Get the String in Specified Base in Golang?
    Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
    2 min read
    How to split a slice of bytes in Golang?
    In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
    3 min read
    Repeating a String for Specific Number of Times in Golang
    In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You are allowed to repeat a string of for a specific number of times with the
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences