
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Split String
The strings.Split() is used to break a string into a list of substrings using a specified delimiter. It returns the substrings in the form of a slice.
Delimiter: The character or pattern used to split a string (e.g., spaces, commas).
Substrings: Smaller strings are obtained after splitting the original string.
Syntax
The syntax of strings.Split() is as follows −
func Split(S string, sep string) []string
Where s is the given string and sep is the delimiter (separator) string. It returns the substrings.
Splitting Strings Using regexp and strings.Split
You can split a string using functions like regexp and strings.Split.
Example
In this example, we splits two strings using both regexp and strings.Split functions. It demonstrates splitting 'p' by the characters 'n' and 's', 'q' by the substring "Lang", and 'p' by each individual character.
package main import ( "fmt" "strings" "regexp" ) func main() { // Intializing the Strings p := "oopsfunctions" q := "GoLang language" // Display the Strings fmt.Println("String 1:", p) fmt.Println("String 2:", q) // Using the Split Function and regexp r := regexp.MustCompile(`[ns]`) s := r.Split(p, -2) t := strings.Split(q, "Lang") u := strings.Split(p, "") // Display the Split Output fmt.Println("Split String 1: ", s) fmt.Println("Split String 2: ", t) fmt.Println("Split String 1 with delimiter:", u) }
Output
It will generate the following output −
String 1: oopsfunctions String 2: GoLang language Split String 1: [oop fu ctio ] Split String 2: [Go language] Split String 1 with delimiter: [o o p s f u n c t i o n s]
Splitting Strings Using Delimiters
You can split a string using different delimiters like !, ,, -, and specific characters.
Example
This example splits three strings 'x', 'y', and 'w' using different delimiters ('!', ',', '-', and 'a'). It demonstrates how to split these strings and prints the resulting substrings.
package main import ( "fmt" "strings" ) func main() { var x string var y string var w string // Intializing the Strings x = "Hello! World" y = "This, is, a, sample, program" w = "1-2-3-4" // Display the Strings fmt.Println("String 1:", x) fmt.Println("String 2:", y) fmt.Println("String 3:", w) // Using the Split Function res1 := strings.Split(x, "!") res2 := strings.Split(y, ",") res3 := strings.Split(w, "-") res4 := strings.Split(y, "a") // Display the Split Output fmt.Println("Split String 1:", res1) fmt.Println("Split String 2:", res2) fmt.Println("Split String 3:", res3) fmt.Println("Split String 2 with delimiter:", res4) }
Output
It will generate the following output −
String 1: Hello! World String 2: This, is, a, sample, program String 3: 1-2-3-4 Split String 1: [Hello World] Split String 2: [This is a sample program] Split String 3: [1 2 3 4] Split String 2 with delimiter: [This, is, , s mple, progr m]