π§΅ Go String Processing: The String Workshop
Imagine you have a magical workshop where words are like LEGO blocks. You can join them, break them apart, transform them, and even peek inside to see what makes them special!
π― What Youβll Learn
In this adventure, weβll explore five powerful tools in Goβs string workshop:
- String Operations β Basic tricks with words
- strings.Builder β The super-fast word assembler
- bytes Package β Working with raw building blocks
- String Conversion β Transforming between types
- Unicode Handling β Understanding magical symbols
π§ 1. String Operations
Think of strings like a necklace of beads. Each bead is a letter. Go gives you tools to work with these necklaces!
The strings Package β Your Toolbox
import "strings"
// Does this necklace contain a red bead?
found := strings.Contains("hello", "ell")
// found = true β
// How many times does "l" appear?
count := strings.Count("hello", "l")
// count = 2
// Does it start with "hel"?
yes := strings.HasPrefix("hello", "hel")
// yes = true β
Common String Operations
| Operation | What It Does | Example |
|---|---|---|
Contains |
Is this inside? | "abc" in "abcdef" β β
|
Count |
How many times? | "a" in "banana" β 3 |
HasPrefix |
Starts with? | "Go" in "Golang" β β
|
HasSuffix |
Ends with? | "ing" in "coding" β β
|
Index |
Where is it? | "o" in "hello" β 4 |
Join |
Glue together | ["a","b","c"] β "a-b-c" |
Split |
Break apart | "a-b-c" β ["a","b","c"] |
ToUpper |
SHOUT IT! | "hi" β "HI" |
ToLower |
whisper it | "HI" β "hi" |
TrimSpace |
Remove edges | " hi " β "hi" |
Replace |
Swap words | "cat" β "bat" |
π Real Example: Clean Up User Input
import "strings"
func cleanInput(s string) string {
// Remove extra spaces
s = strings.TrimSpace(s)
// Make it lowercase
s = strings.ToLower(s)
// Remove bad words
s = strings.ReplaceAll(s, "bad", "***")
return s
}
result := cleanInput(" Hello BAD World ")
// result = "hello *** world"
ποΈ 2. strings.Builder β The Speed Champion
The Problem with + (Glue)
Imagine youβre building a tower with LEGO blocks. Every time you add a block using +, you have to:
- Build a completely new tower
- Copy all the old blocks
- Add the new block
This is SLOW when you have lots of blocks!
// β SLOW way β rebuilds every time!
s := ""
for i := 0; i < 1000; i++ {
s = s + "a" // New string each time!
}
The Solution: strings.Builder
strings.Builder is like a magic expanding box. You just keep throwing blocks in, and it grows automatically!
import "strings"
// β
FAST way β uses Builder!
var builder strings.Builder
for i := 0; i < 1000; i++ {
builder.WriteString("a")
}
result := builder.String()
// "aaaa...a" (1000 a's)
Builder Methods
var b strings.Builder
// Write a string
b.WriteString("Hello")
// Write a single letter (rune)
b.WriteRune(' ')
// Write raw bytes
b.Write([]byte("World"))
// Get the final string
result := b.String()
// result = "Hello World"
// How big is it?
size := b.Len() // 11
// Start over
b.Reset()
π Speed Comparison
Method | 1000 items | 10000 items
-----------------|------------|------------
Using + | 1.5 ms | 150 ms
Using Builder | 0.02 ms | 0.2 ms
π Builder is 100x FASTER!
π¦ 3. bytes Package β Raw Building Blocks
What Are Bytes?
If strings are sentences, bytes are individual letters written in secret code (numbers).
Every letter has a number:
'A'= 65'a'= 97'0'= 48
Working with []byte
// String to bytes
text := "Hello"
data := []byte(text)
// data = [72 101 108 108 111]
// Bytes back to string
back := string(data)
// back = "Hello"
bytes.Buffer β Like strings.Builderβs Twin
import "bytes"
var buf bytes.Buffer
buf.WriteString("Hello")
buf.WriteByte(' ') // Single byte
buf.WriteString("World")
result := buf.String()
// result = "Hello World"
When to Use Which?
| Use Case | Choose |
|---|---|
| Building strings | strings.Builder |
| Working with files/network | bytes.Buffer |
| Need to read AND write | bytes.Buffer |
| Just building text | strings.Builder |
bytes Package Functions
import "bytes"
data := []byte("Hello World")
// Same as strings package!
bytes.Contains(data, []byte("World"))
bytes.Count(data, []byte("l"))
bytes.ToUpper(data)
bytes.Split(data, []byte(" "))
π 4. String Conversion β Shape Shifting
Converting Numbers β Strings
import "strconv"
// Number β String
num := 42
str := strconv.Itoa(num)
// str = "42"
// String β Number
text := "123"
n, err := strconv.Atoi(text)
// n = 123
// Handle errors!
bad := "abc"
_, err := strconv.Atoi(bad)
// err = "invalid syntax"
More Conversions
import "strconv"
// Float β String
f := 3.14159
s := strconv.FormatFloat(f, 'f', 2, 64)
// s = "3.14"
// String β Float
text := "3.14"
val, _ := strconv.ParseFloat(text, 64)
// val = 3.14
// Bool β String
strconv.FormatBool(true) // "true"
// String β Bool
strconv.ParseBool("true") // true, nil
The fmt Package β Easy Mode
import "fmt"
// Anything β String
s := fmt.Sprintf("%d", 42) // "42"
s := fmt.Sprintf("%f", 3.14) // "3.140000"
s := fmt.Sprintf("%.2f", 3.14) // "3.14"
s := fmt.Sprintf("%v", true) // "true"
π― Conversion Cheat Sheet
strconv.Itoa(42) β "42"
strconv.Atoi("42") β 42
fmt.Sprintf("%d", 42) β "42"
fmt.Sprintf("%s", x) β any string
[]byte("hello") β [104 101 108 108 111]
string([]byte{...}) β "hello"
π 5. Unicode Handling β World Languages
The Big Idea
Not all letters are created equal!
'A'= 1 byte'Γ©'= 2 bytes'δΈ'= 3 bytes'π'= 4 bytes
Strings vs Runes
text := "Hello δΈη"
// Length in BYTES (wrong for counting letters!)
len(text) // 12 (not 8!)
// Length in CHARACTERS (runes)
import "unicode/utf8"
utf8.RuneCountInString(text) // 8 β
Whatβs a Rune?
A rune is Goβs name for a single character β any character in any language!
// Rune = character = int32
var r rune = 'δΈ' // Chinese character
fmt.Println(r) // 19990 (its number)
// Loop through characters properly
for _, char := range "Hello δΈη" {
fmt.Printf("%c ", char)
}
// H e l l o δΈ η
The unicode Package
import "unicode"
// Is it a letter?
unicode.IsLetter('A') // true
unicode.IsLetter('5') // false
// Is it a number?
unicode.IsDigit('5') // true
// Is it uppercase?
unicode.IsUpper('A') // true
// Convert case
unicode.ToUpper('a') // 'A'
unicode.ToLower('A') // 'a'
Safe String Processing
import "unicode/utf8"
text := "CafΓ© β"
// Check if valid UTF-8
utf8.ValidString(text) // true
// Get first rune
r, size := utf8.DecodeRuneInString(text)
// r = 'C', size = 1
// Count runes (characters)
count := utf8.RuneCountInString(text)
// count = 6
π¨ Visual: Bytes vs Runes
String: "Go π"
Bytes: [71] [111] [32] [240 159 154 128]
G o space π (4 bytes!)
Runes: [71] [111] [32] [128640]
G o space π
len() = 7 bytes
RuneCount = 4 characters
π§© Putting It All Together
Hereβs a real-world example combining everything:
import (
"strings"
"unicode"
"unicode/utf8"
)
// Clean and process user text
func processText(input string) string {
var b strings.Builder
// Remove extra whitespace
input = strings.TrimSpace(input)
// Process each character
for _, r := range input {
if unicode.IsLetter(r) {
b.WriteRune(unicode.ToLower(r))
} else if unicode.IsSpace(r) {
b.WriteRune(' ')
}
}
return b.String()
}
result := processText(" Hello δΈη! ")
// result = "hello δΈη"
πΊοΈ The Big Picture
graph TD A[String] --> B[String Operations] A --> C[strings.Builder] A --> D[bytes Package] A --> E[Conversion] A --> F[Unicode] B --> B1[Contains/Split/Join] B --> B2[ToUpper/ToLower] B --> B3[Trim/Replace] C --> C1[Fast Building] C --> C2[WriteString] C --> C3[String Result] D --> D1[Raw Bytes] D --> D2[bytes.Buffer] D --> D3[File I/O] E --> E1[strconv Package] E --> E2[Atoi/Itoa] E --> E3[fmt.Sprintf] F --> F1[Runes] F --> F2[utf8 Package] F --> F3[unicode Package]
π‘ Quick Tips
| Situation | Use This |
|---|---|
| Search in string | strings.Contains() |
| Build many strings | strings.Builder |
| Work with files | bytes.Buffer |
| Number to string | strconv.Itoa() |
| String to number | strconv.Atoi() |
| Count characters | utf8.RuneCountInString() |
| Check letter type | unicode.IsLetter() |
π You Did It!
You now know how to:
- β
Manipulate strings with the
stringspackage - β
Build strings efficiently with
strings.Builder - β
Work with raw bytes using the
bytespackage - β Convert between strings and numbers
- β Handle Unicode and international text
Go forth and process those strings! π