Go – Strings Overview
Estimated reading: 3 minutes 24 views

✂️ Go Split String – Divide Strings Using Delimiters (2025 Guide)

🧲 Introduction – How to Split a String in Go?

In Go, you can split a string into substrings using the strings.Split() function from the standard strings package. Whether you’re parsing CSV values, breaking apart words, or handling URLs, Go provides powerful and efficient tools to split strings based on any delimiter.

🎯 In this section, you’ll learn:

  • How to use strings.Split() and SplitN()
  • How to split by space, comma, newline, and other characters
  • Use of Fields() and FieldsFunc() for advanced splitting
  • Real-world examples with CSV, sentences, and custom logic

✅ Basic Syntax – strings.Split()

import "strings"

text := "Go,Java,Python"
parts := strings.Split(text, ",")
fmt.Println(parts)

📤 Output:

[Go Java Python]

strings.Split() returns a slice of substrings split by the given delimiter.


📌 Common Examples

🔹 Split by Space:

s := "Go is awesome"
words := strings.Split(s, " ")
fmt.Println(words)  // [Go is awesome]

🔹 Split by Newline:

lines := strings.Split("line1\nline2\nline3", "\n")
fmt.Println(lines)  // [line1 line2 line3]

🔹 Split CSV:

csv := "apple,banana,grape"
items := strings.Split(csv, ",")
fmt.Println(items)  // [apple banana grape]

🔁 Limit Splits with strings.SplitN()

import "strings"

s := "a-b-c-d"
parts := strings.SplitN(s, "-", 2)
fmt.Println(parts)  // [a b-c-d]

SplitN() limits the number of splits (here: only the first - is used).


📚 Smart Splitting by Whitespace – strings.Fields()

s := "Go    is\tfast"
fields := strings.Fields(s)
fmt.Println(fields)  // [Go is fast]

Fields() splits by any whitespace—space, tab, newline, etc.


🧠 Custom Logic with strings.FieldsFunc()

Split by a custom condition using a function:

import "unicode"

s := "Go1;Go2:Go3"
parts := strings.FieldsFunc(s, func(r rune) bool {
    return r == ';' || r == ':'
})
fmt.Println(parts)  // [Go1 Go2 Go3]

✅ Great for complex delimiters or multi-character logic.


🧠 When to Use What?

FunctionUse When…
Split(s, sep)You know the exact delimiter
SplitN(s, sep, n)You want to limit the number of splits
Fields(s)Split by any whitespace
FieldsFunc(s, fn)Use custom delimiter logic

🧠 Edge Cases

strings.Split("one|", "|")    // ["one", ""]
strings.Split("", ",")        // [""]

✅ Splitting an empty string gives a slice with one empty string ([""]).


📌 Summary – Recap & Next Steps

Go offers versatile string splitting tools through the strings package, letting you handle everything from simple delimiters to custom rules. Whether you’re working with input parsing, logs, or data transformation, Go’s string splitting is fast, clean, and reliable.

🔍 Key Takeaways:

  • Use Split() for basic delimiter splitting
  • Use SplitN() to limit the number of splits
  • Use Fields() to split on any whitespace
  • Use FieldsFunc() for custom split logic

⚙️ Next: Explore Go String Formatting to style and structure string output using format verbs.


❓ FAQs – Go Split String

❓ What does strings.Split() return in Go?
✅ It returns a slice of substrings, split by the provided delimiter.

❓ How do I split a string by whitespace in Go?
✅ Use strings.Fields() to split by space, tab, newline, etc.

❓ What’s the difference between Split() and SplitN()?
Split() splits on all occurrences. SplitN() lets you limit how many times it splits.

❓ How can I split a string with multiple delimiters?
✅ Use strings.FieldsFunc() with a custom function to define your rules.

❓ Does Go support regex-based string splitting?
❌ Not directly in the strings package. For regex, use regexp.Split() from the regexp package.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Split String

Or Copy Link

CONTENTS
Scroll to Top