• Strings packages cheatsheet for packages for basic processing and conversion of strings.

Basic

s := "My string"

Length

len(s)

Iterate over characters

for _, ch := range s {
    fmt.Print(string(ch), ",")
}
fmt.PrintLn()

Comparison

Use <, >, ==, != operators and get true or false.

"dog" < "cat"

Use the compare function to get -1, 0, or 1 as result.

result := strings.Compare("dog", "cat")

Case-insensitive comparison.

strings.EqualFold("Hello", "hello")
// true

Change case

strings.ToUpper(s)
strings.ToLower(s)
strings.ToTitle(s)

Manipulation

Split

Split by string

e.g.

strings.Split(s, " ")
strings.Split(s, ",")
strings.Split(s, "the")

Recommended - use quoting of output for readability.

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "my string"
	fmt.Printf("%q\n", strings.Split(s, " "))
}

Split by whitespace

From tutorial.

Using strings.Fields will split by whitespace.

strings.Fields(myString)  

e.g.

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  myString := "Foo bar bazz"
  myArray := strings.Fields(myString)  
  
  fmt.Println(myArray)
  
  for _, v := range myArray {    
    fmt.Println(v)
  }
}
// Foo
// bar
// bazz

If you just print myArray, it appears to only have on element but that’s just a representation issue because of lack of commas.

Join

strings.Join(myValues, " - ")

Replace

rep := strings.NewReplacer(".", "|")

Or supply multiple pairs.

rep := strings.NewReplacer(".", "|", ",", "|")
rep.Replace(s)

Term

Check for entire string.

strings.Contains(s, "search term")

Character

Check if any of the characters appears.

strings.ContainsAny(s, "abcd")

Index

Find the first instance of a substring. e.g. 0 for start or -1 for not found.

strings.Index(s, "fox")

Or IndexAny for characters.

Starts and ends with

strings.HasPrefix(s, "my prefix")

strings.HasSuffix(s, "my suffix")

Count

Number of non-overlapping substrings occurring.

strings.Count(s, "my term")

Conversion

The most common numeric conversions are Atoi (string to int) and Itoa (int to string).

i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)

Parse

Convert string to other types.

Using 64-bit and base 10 (for integers).

b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)

e.g.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	x := "123"
	y, _ := strconv.ParseInt(x, 10, 32)
	fmt.Printf("%v\n", y)
}

Format

Convert other types to strings.

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

Quote

Add quotes. Unicode characters are escaped with \u.

q := strconv.Quote("Hello, 世界")
fmt.Println("Result", q)
// Result "Hello, 世界"

q = strconv.QuoteToASCII("Hello, 世界")
fmt.Println("Result", q)
// Result "Hello, \u4e16\u754c"

Bytes to string

If you print bytes you’ll see something like this:

fmt.Println(content)
// [123 10 32 32 34 97]

Use string function:

fmt.Println(string(content))

Or format as a string with Printf instead:

fmt.Printf("%s\n", content)