23 September 2017
fmt.Printf("%v\n", strings.Compare("abc", "abc")) // 0
fmt.Printf("%v\n", strings.Compare("abc", "ab"))  // 1
fmt.Printf("%v\n", strings.Compare("ab", "abc"))  // -1

fmt.Printf("%v\n", strings.Contains("abc", "ab")) // true
fmt.Printf("%v\n", strings.Contains("abc", "d"))  // false

fmt.Printf("%v\n", strings.ContainsAny("abc", "a"))  // true
fmt.Printf("%v\n", strings.ContainsAny("abc", "d"))  // false
fmt.Printf("%v\n", strings.ContainsAny("abc", "da")) // true
fmt.Printf("%v\n", strings.ContainsAny("", ""))      // false

fmt.Printf("%v\n", strings.ContainsRune("abc", 97)) // true, 97 => a

fmt.Printf("%v\n", strings.Count("abc", "c"))   // 1
fmt.Printf("%v\n", strings.Count("acc", "c"))   // 2
fmt.Printf("%v\n", strings.Count("abc", ""))    // 4
fmt.Printf("%v\n", strings.Count("cccc", "cc")) // 2

fmt.Printf("%v\n", strings.EqualFold("Go", "go"))  // true
fmt.Printf("%v\n", strings.EqualFold("Go", "GO"))  // true
fmt.Printf("%v\n", strings.EqualFold("Go", "Go1")) // false

fmt.Printf("%q\n", strings.Fields("foo bar baz"))       //["foo" "bar" "baz"]
fmt.Printf("%q\n", strings.Fields("foo,bar,baz"))       //["foo,bar,baz"]
fmt.Printf("%q\n", strings.Fields("foo, bar, baz"))     //["foo," "bar," "baz"]
fmt.Printf("%q\n", strings.Fields("  foo bar  baz   ")) //["foo" "bar" "baz"]

fieldF := func(c rune) bool {
        return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
fmt.Printf("%q\n", strings.FieldsFunc("  foo1;bar2,baz3...", fieldF)) // ["foo1" "bar2" "baz3"]

fmt.Printf("%v\n", strings.HasPrefix("Gopher", "Go")) // true
fmt.Printf("%v\n", strings.HasPrefix("Gopher", "C"))  // false
fmt.Printf("%v\n", strings.HasPrefix("Gopher", ""))   // true

fmt.Printf("%v\n", strings.HasSuffix("Amigo", "go")) // true
fmt.Printf("%v\n", strings.HasSuffix("Amigo", "O"))  // false
fmt.Printf("%v\n", strings.HasSuffix("Amigo", ""))   // true

fmt.Printf("%v\n", strings.Index("go gopher", "go"))     // 0
fmt.Printf("%v\n", strings.LastIndex("go gopher", "go")) // 3
fmt.Printf("%v\n", strings.Index("chicken", "dmr"))      // -1
fmt.Printf("%v\n", strings.LastIndex("chicken", "dmr"))  // -1

fmt.Printf("%v\n", strings.IndexAny("chicken", "aeiouy")) // 2
fmt.Printf("%v\n", strings.IndexAny("crwth", "aeiouy"))   // -1
// LastIndexAny

fmt.Printf("%v\n", strings.IndexByte("golang", 'g'))  // 0
fmt.Printf("%v\n", strings.IndexByte("gophers", 'z')) // -1
// LastIndexByte

indexF := func(c rune) bool {
        return unicode.Is(unicode.Han, c)
}
fmt.Printf("%v\n", strings.IndexFunc("Hello, 世界", indexF))    // 7
fmt.Printf("%v\n", strings.IndexFunc("Hello, world", indexF)) // -1
// LastIndexFunc

fmt.Printf("%v\n", strings.Join([]string{"foo", "bar", "baz"}, ", ")) // foo, bar, baz

rot13 := func(r rune) rune {
        switch {
        case r >= 'A' && r <= 'Z':
                return 'A' + (r-'A'+13)%26
        case r >= 'a' && r <= 'z':
                return 'a' + (r-'a'+13)%26
        }
        return r
}
fmt.Printf("%v\n", strings.Map(rot13, "'Twas brillig and the slithy gopher..."))

fmt.Printf("%v\n", "ba"+strings.Repeat("na", 2))

fmt.Printf("%v\n", strings.Replace("oink oink oink", "oink", "moo", 2))  // moo moo oink
fmt.Printf("%v\n", strings.Replace("oink oink oink", "oink", "moo", -1)) // moo moo moo

fmt.Printf("%q\n", strings.Split("a,b,c", ","))             // ["a" "b" "c"]
fmt.Printf("%q\n", strings.Split(" xyz ", ""))              // [" " "x" "y" "z" " "]
fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) // [""]

fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) // ["a," "b," "c"]

fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", -1))     // ["a" "b" "c"]
fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))      // ["a" "b,c"]
fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) // ["a," "b,c"]

fmt.Printf("%v\n", strings.Title("her royal highness")) // Her Royal Highness
fmt.Printf("%v\n", strings.ToTitle("loud noises"))      // LOUD NOISES
fmt.Printf("%v\n", strings.ToLower("Gopher"))
fmt.Printf("%v\n", strings.ToUpper("Gopher"))

// ToTitleSpecial(c unicode.SpecialCase, s string)
// ToLowerSpecial(c unicode.SpecialCase, s string)
// ToUpperSpecial(c unicode.SpecialCase, s string)

fmt.Printf("###%v###\n", " !!! Achtung! Achtung! !!! ")                     // ### !!! Achtung! Achtung! !!! ###
fmt.Printf("###%v###\n", strings.Trim(" !!! Achtung! Achtung! !!! ", " "))  // ###!!! Achtung! Achtung! !!!###
fmt.Printf("###%v###\n", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) // ###Achtung! Achtung###

trimF := func(c rune) bool {
        return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
fmt.Printf("###%v###\n", strings.TrimFunc("  Achtung1! Achtung2,...", trimF))

// TrimLeft
// TrimLeftFunc
// TrimRightFunc
// TrimRightFunc

// TrimPrefix(str, prefix) 删除一串字符
// TrimSuffix(str, prefix) 删除一串字符

fmt.Printf("###%v###\n", strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) // 删除左右的空字符

Reader

A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner, and io.RuneScanner interfaces by reading from a string.

func NewReader(s string) *Reader
func (r *Reader) Len() int
func (r *Reader) Read(b []byte) (n int, err error)
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
func (r *Reader) ReadByte() (byte, error)
func (r *Reader) ReadRune() (ch rune, size int, err error)
func (r *Reader) Reset(s string)
func (r *Reader) Seek(offset int64, whence int) (int64, error)
func (r *Reader) Size() int64
func (r *Reader) UnreadByte() error
func (r *Reader) UnreadRune() error
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

Replacer

Replacer replaces a list of strings with replacements. It is safe for concurrent use by multiple goroutines.

func NewReplacer(oldnew …string) *Replacer

r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
fmt.Println(r.Replace("This is <b>HTML</b>!")) // This is &lt;b&gt;HTML&lt;/b&gt;!

func (r *Replacer) Replace(s string) string

r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
fmt.Println(r.Replace("This is <b>HTML</b>!")) // This is &lt;b&gt;HTML&lt;/b&gt;!

func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)

r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
r.WriteString(os.Stdout, "This is <b>HTML</b>!") // This is &lt;b&gt;HTML&lt;/b&gt;!