Skip to content

Latest commit

 

History

History
157 lines (117 loc) · 3.96 KB

File metadata and controls

157 lines (117 loc) · 3.96 KB

#7.6 Strings Almost everything we see is represented by string, so it's a very important part of web development, including user inputs, database access; also we need to split, join and convert strings in many cases. In this section, we are going to introduce packages strings and strconv in Go standard library.

##strings Following functions are from package strings, more details please see official documentation:

  • func Contains(s, substr string) bool

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

      fmt.Println(strings.Contains("seafood", "foo"))
      fmt.Println(strings.Contains("seafood", "bar"))
      fmt.Println(strings.Contains("seafood", ""))
      fmt.Println(strings.Contains("", ""))
      //Output:
      //true
      //false
      //true
      //true
  • func Join(a []string, sep string) string

    Combine strings from slice with separator sep.

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

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

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

    Repeat string s with count times.

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

    Replace string old with string new in string s, n means replication times, if n less than 0 means replace all.

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

    Split string s with separator sep into a slice.

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

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

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

    Remove space items and split string with space in to a slice.

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

##strconv Following functions are from package strconv, more details please see official documentation:

  • Append series convert data to string and append to current byte slice.

      package main
      
      import (
      	"fmt"
      	"strconv"
      )
      
      func main() {
      	str := make([]byte, 0, 100)
      	str = strconv.AppendInt(str, 4567, 10)
      	str = strconv.AppendBool(str, false)
      	str = strconv.AppendQuote(str, "abcdefg")
      	str = strconv.AppendQuoteRune(str, '单')
      	fmt.Println(string(str))
      }
  • Format series convert other type data to string.

      package main
    
      import (
      	"fmt"
      	"strconv"
      )
      
      func main() {
      	a := strconv.FormatBool(false)
      	b := strconv.FormatFloat(123.23, 'g', 12, 64)
      	c := strconv.FormatInt(1234, 10)
      	d := strconv.FormatUint(12345, 10)
      	e := strconv.Itoa(1023)
      	fmt.Println(a, b, c, d, e)
      }
  • Parse series convert string to other types.

      package main
    
      import (
      	"fmt"
      	"strconv"
      )
    
      func main() {
      	a, err := strconv.ParseBool("false")
      	if err != nil {
      		fmt.Println(err)
      	}
      	b, err := strconv.ParseFloat("123.23", 64)
      	if err != nil {
      		fmt.Println(err)
      	}
      	c, err := strconv.ParseInt("1234", 10, 64)
      	if err != nil {
      		fmt.Println(err)
      	}
      	d, err := strconv.ParseUint("12345", 10, 64)
      	if err != nil {
      		fmt.Println(err)
      	}
      	e, err := strconv.Itoa("1023")
      	if err != nil {
      		fmt.Println(err)
      	}
      	fmt.Println(a, b, c, d, e)
      }

##Links