Tech

String Manipulation in Go: How to Update Text


String manipulation is fundamental in software development; most programming languages provide a string type, after all. A string is a sequence of characters: letters, numbers, and symbols.


String manipulation comes in handy for various applications, from text processing and data analysis to web development. Popular string manipulation operations are concatenation, data validation, extraction, and formatting. Go provides a string manipulation package named “strings” in the standard library.


The strings Package

The strings package provides various useful functions for string manipulation and other operations. The package includes functionality for substring operations, trimming, comparing strings, string conversion, string building, splitting, and more.

You can import the strings package by specifying the package name in your list of imports.

 import "strings"

Searching for Substrings

The strings package provides three functions for searching for substrings: the Contains function, the ContainsAny function, and the ContainsRune function.

The Contains function checks if the specified string contains the substring. The ContainsAny function checks if the string contains any characters in the substring, and the ContainsRune function checks if the string contains a rune (a Unicode character).

 import (
    "fmt"
    "strings"
)

func main() {
    aString := "Hello, World!"
    substring := "World"
    characters := "aeiou"
    aRune := 'o'

    fmt.Println(strings.Contains(aString, substring))
    fmt.Println(strings.ContainsAny(aString, characters))
    fmt.Println(strings.ContainsRune(aString, aRune))
}

You can retrieve the index of a substring with the Index, IndexAny, IndexByte, and IndexFunc functions. The Index function returns the index of a substring, if it occurs in another given string. The IndexAny function returns the index of the first instance of a Unicode code point or -1 if none of the characters is present.

 import (
    "fmt"
    "strings"
)

func main() {
    aString := "Hello, world!"
    substring := "world"
    chars := "wrld"
    byteCharacter := byte('o')
    aRune := rune('o')

    fmt.Println(strings.Index(aString, substring))
    fmt.Println(strings.IndexAny(aString, chars))
    fmt.Println(strings.IndexByte(aString, byteCharacter))

    f := func(r rune) bool {
        return r == 'o'
    }
    
    fmt.Println(strings.IndexFunc(aString, f))
    fmt.Println(strings.IndexRune(aString, aRune))
}

IndexByte returns the index of the first instance of the byte character in the string or -1. The IndexFunc function returns the index into the string of the first Unicode point that satisfies a given function. Finally, the IndexRune function returns the index of the first instance of the Unicode code point of the rune.

Replacing Substrings in Go

The Replace and ReplaceAll function help with replacing substrings. The Replace function takes in the string, the original substring, and the replacement and several replacements. The ReplaceAll function takes in only the string, the initial substring, and the replacement.

 import (
    "fmt"
    "strings"
)

func main() {
    theString := "This is a test string to be modified."
    fmt.Println(strings.Replace(theString, "is", "was", 1))
    fmt.Println(strings.Replace(theString, "is", "was", -1))
    fmt.Println(strings.ReplaceAll(theString, "is", "was"))
}

Notice that you can use both Replace and ReplaceAll to replace every occurrence within the string.

result from Go replacement operation

Splitting and Joining Strings

The strings package contains the Split, SplitAfter, SplitAfterN, and SplitN functions for splitting strings that return a slice of the strings.

The Split method splits by a specified delimiter. Similar to the Split method, the SplitAfter method splits the string, but it includes the separator in its results.

 import (
    "fmt"
    "strings"
)

func main() {
    s := "This is a test string to be split."

    fmt.Println(strings.Split(s, " "))
    fmt.Println(strings.SplitAfter(s, " "))
    fmt.Println(strings.SplitAfterN(s, " ", 3))
    fmt.Println(strings.SplitN(s, " ", 3))
}

The SplitAfterN method is similar to the SplitAfter function except that the function splits the string into a specified maximum of substrings. The SplitN method splits the string to a specified maximum number without including the separator in the substrings.

You can join strings with the Join function from the strings package. The Join function takes in a slice and a delimiter.

 import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Join([]string{"Hello", "World"}, ":"))
   
}

Manipulating String Case

Manipulating string case is handy for many tasks, including working with documentation. You can use the ToLower function for lowercasing, the ToUpper function for uppercasing, and the ToTitle function for title casing.

 import (
    "fmt"
    "strings"
)

func main() {
    s := "This is a test string."

    fmt.Println(strings.ToLower(s))
    fmt.Println(strings.ToUpper(s))
    fmt.Println(strings.ToTitle(s))
}

String Building in Go

String builders are a type that allows for efficient concatenation in Go. The bytes.Buffer type is one of the commonly used string builders. The bytes.Buffer method implements a growable buffer of bytes with read and write methods for operations, allowing for efficient string appends without needing to make new copies, unlike the + operation and the Join function.

 import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder

    
    b.WriteString("This ")
    b.WriteString("is ")
    b.WriteString("a ")
    b.WriteString("test ")
    b.WriteString("string.")

    
    fmt.Println(b.Len())

    
    str := b.String()
    fmt.Println(str)

    
    b.Reset()

    
    b.WriteString("This ")
    b.WriteString("is ")
    b.WriteString("another ")
    b.WriteString("test ")
    b.WriteString("string.")

    
    fmt.Println(b.Cap())

    
    str = b.String()
    fmt.Println(str)
}

The main function demonstrates how you can use the strings.Builder type for efficient string building. The WriteString method of the Strings.Builder type extends the sequence of strings to one another, and the Len method returns the length of the built string.

The String method converts the builder’s content to a string, and the Reset method resets the builder for additional string building.

The Cap method returns the capacity of the builder. This is the current space that Go has allocated for the string.

result from string building operation

String Trimming in Go

The strings package also provides functionality for trimming strings in the Trim, TrimLeft, TrimPrefix, TrimRight, TrimSpace, and TrimSuffix functions.

 import (
    "strings"
    "fmt"
)

func main() {
    
    s := "Hello, World!"

    
    prefix := "Hello"
    suffix := "World!"

    
    fmt.Println(strings.Trim(s, "!"))

    
    fmt.Println(strings.TrimSpace(s))

    
    fmt.Println(strings.TrimLeft(s, "Hello"))

    
    fmt.Println(strings.TrimRight(s, "World!"))

    
    fmt.Println(strings.TrimPrefix(s, prefix))

    
    fmt.Println(strings.TrimSuffix(s, suffix))
}

The main function demonstrates how you can use the functions for trimming. The different functions trim the s string in various ways:

result from trimming operation

You Can Format Strings in Go

The Go standard library also provides the fmt package for string formatting. The fmt package uses C-style formatting verbs for efficient string formatting in Go.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button