Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strings.SplitAfter() Function in Golang
strings.SplitAfter() is a built-in function in Golang that is used to break a string into a slice. SplitAfter is different from other Split functions. Here, we slice a given string into substrings after each instance of separators and it returns a slice of those substrings.
Syntax
func SplitAfter(S String, sep string) []string
Where s is the given string and sep is the separator string.
Example 1
Consider the following example −
package main
import (
"fmt"
"strings"
)
func main() {
// Intializing the Strings
x := "Golang Program of SplitAfter Function"
y := "1.2.3.4.5.6.7.8"
// Display the Strings
fmt.Println("String 1: ", x)
fmt.Println("String 2: ", y)
// Using the SplitAfter
z := strings.SplitAfter(x, "r")
w := strings.SplitAfter(y, ".")
// Display the SplitAfter Output
fmt.Println("\nSplitAfter for String 1 \n:", z)
// range() Function
for v := range(w) {
fmt.Println("\nString 2 Range:", w[v])
}
}
Output
It will generate the following output −
String 1: Golang Program of SplitAfter Function String 2: 1.2.3.4.5.6.7.8 SplitAfter for String 1 : [Golang Pr ogr am of SplitAfter Function] String 2 Range: 1. String 2 Range: 2. String 2 Range: 3. String 2 Range: 4. String 2 Range: 5. String 2 Range: 6. String 2 Range: 7. String 2 Range: 8
Example 2
Let us take another example −
package main
import (
"fmt"
"strings"
)
func main() {
// Intializing the Strings
str1 := "012344564784891423156"
str2 := "Golang SplitAfter String Function"
str3 := "1&2&3&4&5&6&7&8"
// Display the Strings
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
fmt.Println("String 3: ", str3)
// Using the SplitAfter Function
res1 := strings.SplitAfter(str1, "4")
res2 := strings.SplitAfter(str2, "Split")
res3 := strings.SplitAfter(str3, "&")
res4 := strings.SplitAfter(str2, " ")
res5 := strings.SplitAfter(str2, "Go")
// Display the SplitAfter Output
fmt.Println("SplitAfter for String 1:", res1)
fmt.Println("SplitAfter for String 2:", res2)
fmt.Println("SplitAfter for String 3:", res3)
fmt.Println("SplitAfter for String 2:", res4)
fmt.Println("SplitAfter for String 2:", res5)
}
Output
It will generate the following output −
String 1: 012344564784891423156 String 2: Golang SplitAfter String Function String 3: 1&2&3&4&5&6&7&8 SplitAfter for String 1: [01234 4 564 784 8914 23156] SplitAfter for String 2: [Golang Split After String Function] SplitAfter for String 3: [1& 2& 3& 4& 5& 6& 7& 8] SplitAfter for String 2: [Golang SplitAfter String Function] SplitAfter for String 2: [Go lang SplitAfter String Function]
Advertisements