String Processing

Loading concept...

🧡 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:

  1. String Operations β€” Basic tricks with words
  2. strings.Builder β€” The super-fast word assembler
  3. bytes Package β€” Working with raw building blocks
  4. String Conversion β€” Transforming between types
  5. 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:

  1. Build a completely new tower
  2. Copy all the old blocks
  3. 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 strings package
  • βœ… Build strings efficiently with strings.Builder
  • βœ… Work with raw bytes using the bytes package
  • βœ… Convert between strings and numbers
  • βœ… Handle Unicode and international text

Go forth and process those strings! πŸš€

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.