7.6 Strings

On the web, almost everything we see (including user inputs, database access, etc.), is represented by strings. They are a very important part of web development. In many cases, we also need to split, join, convert and otherwise manipulate strings. In this section, we are going to introduce the strings and strconv packages from the Go standard library.

strings

The following functions are from the strings package. See the official documentation for more details:

  • func Contains(s, substr string) bool

    Check if string s contains string substr, returns a boolean value.

    1. fmt.Println(strings.Contains("seafood", "foo"))
    2. fmt.Println(strings.Contains("seafood", "bar"))
    3. fmt.Println(strings.Contains("seafood", ""))
    4. fmt.Println(strings.Contains("", ""))
    5. //Output:
    6. //true
    7. //false
    8. //true
    9. //true
  • func Join(a []string, sep string) string

    Combine strings from slice with separator sep.

    1. s := []string{"foo", "bar", "baz"}
    2. fmt.Println(strings.Join(s, ", "))
    3. //Output:foo, bar, baz
  • func Index(s, sep string) int

    Find index of sep in string s, returns -1 if it’s not found.

    1. fmt.Println(strings.Index("chicken", "ken"))
    2. fmt.Println(strings.Index("chicken", "dmr"))
    3. //Output:4
    4. //-1
  • func Repeat(s string, count int) string

    Repeat string s count times.

    1. fmt.Println("ba" + strings.Repeat("na", 2))
    2. //Output:banana
  • func Replace(s, old, new string, n int) string

    Replace string old with string new in string s. n is the number of replacements. If n is less than 0, replace all instances.

    1. fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
    2. fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
    3. //Output:oinky oinky oink
    4. //moo moo moo
  • func Split(s, sep string) []string

    Split string s with separator sep into a slice.

    1. fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    2. fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    3. fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    4. fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
    5. //Output:["a" "b" "c"]
    6. //["" "man " "plan " "canal panama"]
    7. //[" " "x" "y" "z" " "]
    8. //[""]
  • func Trim(s string, cutset string) string

    Remove cutset of string s if it’s leftmost or rightmost.

    1. fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
    2. Output:["Achtung"]
  • func Fields(s string) []string

    Remove space items and split string with space into a slice.

    1. fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
    2. //Output:Fields are: ["foo" "bar" "baz"]

strconv

The following functions are from the strconv package. As usual, please see official documentation for more details:

  • Append series, convert data to string, and append to current byte slice.
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. str := make([]byte, 0, 100)
  8. str = strconv.AppendInt(str, 4567, 10)
  9. str = strconv.AppendBool(str, false)
  10. str = strconv.AppendQuote(str, "abcdefg")
  11. str = strconv.AppendQuoteRune(str, '单')
  12. fmt.Println(string(str))
  13. }
  • Format series, convert other data types into string.
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. a := strconv.FormatBool(false)
  8. b := strconv.FormatFloat(123.23, 'g', 12, 64)
  9. c := strconv.FormatInt(1234, 10)
  10. d := strconv.FormatUint(12345, 10)
  11. e := strconv.Itoa(1023)
  12. fmt.Println(a, b, c, d, e)
  13. }
  • Parse series, convert strings to other types.
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. a, err := strconv.ParseBool("false")
  8. if err != nil {
  9. fmt.Println(err)
  10. }
  11. b, err := strconv.ParseFloat("123.23", 64)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. c, err := strconv.ParseInt("1234", 10, 64)
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. d, err := strconv.ParseUint("12345", 10, 64)
  20. if err != nil {
  21. fmt.Println(err)
  22. }
  23. e, err := strconv.Itoa("1023")
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. fmt.Println(a, b, c, d, e)
  28. }