Remove 07.6.md spaces

This commit is contained in:
vCaesar
2017-06-09 22:41:25 +08:00
parent 2301921ef3
commit 43e55a177b

View File

@@ -10,76 +10,76 @@ The following functions are from the `strings` package. See the official documen
Check if string `s` contains string `substr`, returns a boolean value. Check if string `s` contains string `substr`, returns a boolean value.
```Go ```Go
fmt.Println(strings.Contains("seafood", "foo")) fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar")) fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", "")) fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", "")) fmt.Println(strings.Contains("", ""))
//Output: //Output:
//true //true
//false //false
//true //true
//true //true
``` ```
- func Join(a []string, sep string) string - func Join(a []string, sep string) string
Combine strings from slice with separator `sep`. Combine strings from slice with separator `sep`.
```Go ```Go
s := []string{"foo", "bar", "baz"} s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", ")) fmt.Println(strings.Join(s, ", "))
//Output:foo, bar, baz //Output:foo, bar, baz
``` ```
- func Index(s, sep string) int - func Index(s, sep string) int
Find index of `sep` in string `s`, returns -1 if it's not found. Find index of `sep` in string `s`, returns -1 if it's not found.
```Go ```Go
fmt.Println(strings.Index("chicken", "ken")) fmt.Println(strings.Index("chicken", "ken"))
fmt.Println(strings.Index("chicken", "dmr")) fmt.Println(strings.Index("chicken", "dmr"))
//Output:4 //Output:4
//-1 //-1
``` ```
- func Repeat(s string, count int) string - func Repeat(s string, count int) string
Repeat string `s` `count` times. Repeat string `s` `count` times.
```Go ```Go
fmt.Println("ba" + strings.Repeat("na", 2)) fmt.Println("ba" + strings.Repeat("na", 2))
//Output:banana //Output:banana
``` ```
- func Replace(s, old, new string, n int) string - 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. Replace string `old` with string `new` in string `s`. `n` is the number of replacements. If n is less than 0, replace all instances.
```Go ```Go
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
//Output:oinky oinky oink //Output:oinky oinky oink
//moo moo moo //moo moo moo
``` ```
- func Split(s, sep string) []string - func Split(s, sep string) []string
Split string `s` with separator `sep` into a slice. Split string `s` with separator `sep` into a slice.
```Go ```Go
fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 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("a man a plan a canal panama", "a "))
fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split(" xyz ", ""))
fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
//Output:["a" "b" "c"] //Output:["a" "b" "c"]
//["" "man " "plan " "canal panama"] //["" "man " "plan " "canal panama"]
//[" " "x" "y" "z" " "] //[" " "x" "y" "z" " "]
//[""] //[""]
``` ```
- func Trim(s string, cutset string) string - func Trim(s string, cutset string) string
Remove `cutset` of string `s` if it's leftmost or rightmost. Remove `cutset` of string `s` if it's leftmost or rightmost.
```Go ```Go
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! ")) fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
Output:["Achtung"] Output:["Achtung"]
``` ```
- func Fields(s string) []string - func Fields(s string) []string
Remove space items and split string with space into a slice. Remove space items and split string with space into a slice.
```Go ```Go
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
//Output:Fields are: ["foo" "bar" "baz"] //Output:Fields are: ["foo" "bar" "baz"]
``` ```
## strconv ## strconv
@@ -88,72 +88,72 @@ The following functions are from the `strconv` package. As usual, please see off
- Append series, convert data to string, and append to current byte slice. - Append series, convert data to string, and append to current byte slice.
```Go ```Go
package main package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
) )
func main() { func main() {
str := make([]byte, 0, 100) str := make([]byte, 0, 100)
str = strconv.AppendInt(str, 4567, 10) str = strconv.AppendInt(str, 4567, 10)
str = strconv.AppendBool(str, false) str = strconv.AppendBool(str, false)
str = strconv.AppendQuote(str, "abcdefg") str = strconv.AppendQuote(str, "abcdefg")
str = strconv.AppendQuoteRune(str, '单') str = strconv.AppendQuoteRune(str, '单')
fmt.Println(string(str)) fmt.Println(string(str))
} }
``` ```
- Format series, convert other data types into string. - Format series, convert other data types into string.
```Go ```Go
package main package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
) )
func main() { func main() {
a := strconv.FormatBool(false) a := strconv.FormatBool(false)
b := strconv.FormatFloat(123.23, 'g', 12, 64) b := strconv.FormatFloat(123.23, 'g', 12, 64)
c := strconv.FormatInt(1234, 10) c := strconv.FormatInt(1234, 10)
d := strconv.FormatUint(12345, 10) d := strconv.FormatUint(12345, 10)
e := strconv.Itoa(1023) e := strconv.Itoa(1023)
fmt.Println(a, b, c, d, e) fmt.Println(a, b, c, d, e)
} }
``` ```
- Parse series, convert strings to other types. - Parse series, convert strings to other types.
```Go ```Go
package main package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
) )
func main() { func main() {
a, err := strconv.ParseBool("false") a, err := strconv.ParseBool("false")
if err != nil { if err != nil {
fmt.Println(err) 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)
} }
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 ## Links