2012-02-09 09:19:58 +01:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package strings_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-06-05 01:15:33 +02:00
|
|
|
"unicode"
|
2012-02-09 09:19:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleFields() {
|
|
|
|
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: Fields are: ["foo" "bar" "baz"]
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
func ExampleFieldsFunc() {
|
|
|
|
f := func(c rune) bool {
|
|
|
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
|
|
|
}
|
|
|
|
fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
|
|
|
|
// Output: Fields are: ["foo1" "bar2" "baz3"]
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
func ExampleContains() {
|
|
|
|
fmt.Println(strings.Contains("seafood", "foo"))
|
|
|
|
fmt.Println(strings.Contains("seafood", "bar"))
|
|
|
|
fmt.Println(strings.Contains("seafood", ""))
|
|
|
|
fmt.Println(strings.Contains("", ""))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// true
|
|
|
|
// false
|
|
|
|
// true
|
|
|
|
// true
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleContainsAny() {
|
|
|
|
fmt.Println(strings.ContainsAny("team", "i"))
|
|
|
|
fmt.Println(strings.ContainsAny("failure", "u & i"))
|
|
|
|
fmt.Println(strings.ContainsAny("foo", ""))
|
|
|
|
fmt.Println(strings.ContainsAny("", ""))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// false
|
|
|
|
// true
|
|
|
|
// false
|
|
|
|
// false
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleCount() {
|
|
|
|
fmt.Println(strings.Count("cheese", "e"))
|
|
|
|
fmt.Println(strings.Count("five", "")) // before & after each rune
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// 3
|
|
|
|
// 5
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleEqualFold() {
|
|
|
|
fmt.Println(strings.EqualFold("Go", "go"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: true
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
func ExampleHasPrefix() {
|
|
|
|
fmt.Println(strings.HasPrefix("Gopher", "Go"))
|
|
|
|
fmt.Println(strings.HasPrefix("Gopher", "C"))
|
|
|
|
fmt.Println(strings.HasPrefix("Gopher", ""))
|
|
|
|
// Output:
|
|
|
|
// true
|
|
|
|
// false
|
|
|
|
// true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleHasSuffix() {
|
|
|
|
fmt.Println(strings.HasSuffix("Amigo", "go"))
|
|
|
|
fmt.Println(strings.HasSuffix("Amigo", "O"))
|
|
|
|
fmt.Println(strings.HasSuffix("Amigo", "Ami"))
|
|
|
|
fmt.Println(strings.HasSuffix("Amigo", ""))
|
|
|
|
// Output:
|
|
|
|
// true
|
|
|
|
// false
|
|
|
|
// false
|
|
|
|
// true
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
func ExampleIndex() {
|
|
|
|
fmt.Println(strings.Index("chicken", "ken"))
|
|
|
|
fmt.Println(strings.Index("chicken", "dmr"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// 4
|
|
|
|
// -1
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
func ExampleIndexFunc() {
|
|
|
|
f := func(c rune) bool {
|
|
|
|
return unicode.Is(unicode.Han, c)
|
|
|
|
}
|
|
|
|
fmt.Println(strings.IndexFunc("Hello, 世界", f))
|
|
|
|
fmt.Println(strings.IndexFunc("Hello, world", f))
|
|
|
|
// Output:
|
|
|
|
// 7
|
|
|
|
// -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleIndexAny() {
|
|
|
|
fmt.Println(strings.IndexAny("chicken", "aeiouy"))
|
|
|
|
fmt.Println(strings.IndexAny("crwth", "aeiouy"))
|
|
|
|
// Output:
|
|
|
|
// 2
|
|
|
|
// -1
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func ExampleIndexRune() {
|
2012-03-02 17:38:43 +01:00
|
|
|
fmt.Println(strings.IndexRune("chicken", 'k'))
|
|
|
|
fmt.Println(strings.IndexRune("chicken", 'd'))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// 4
|
|
|
|
// -1
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleLastIndex() {
|
|
|
|
fmt.Println(strings.Index("go gopher", "go"))
|
|
|
|
fmt.Println(strings.LastIndex("go gopher", "go"))
|
|
|
|
fmt.Println(strings.LastIndex("go gopher", "rodent"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// 0
|
|
|
|
// 3
|
|
|
|
// -1
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleJoin() {
|
|
|
|
s := []string{"foo", "bar", "baz"}
|
|
|
|
fmt.Println(strings.Join(s, ", "))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: foo, bar, baz
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRepeat() {
|
|
|
|
fmt.Println("ba" + strings.Repeat("na", 2))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: banana
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleReplace() {
|
|
|
|
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
|
|
|
|
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// oinky oinky oink
|
|
|
|
// moo moo moo
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleSplit() {
|
|
|
|
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"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// ["a" "b" "c"]
|
|
|
|
// ["" "man " "plan " "canal panama"]
|
|
|
|
// [" " "x" "y" "z" " "]
|
|
|
|
// [""]
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleSplitN() {
|
|
|
|
fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
|
|
|
|
z := strings.SplitN("a,b,c", ",", 0)
|
|
|
|
fmt.Printf("%q (nil = %v)\n", z, z == nil)
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// ["a" "b,c"]
|
|
|
|
// [] (nil = true)
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleSplitAfter() {
|
|
|
|
fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: ["a," "b," "c"]
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleSplitAfterN() {
|
|
|
|
fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: ["a," "b,c"]
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleTitle() {
|
|
|
|
fmt.Println(strings.Title("her royal highness"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: Her Royal Highness
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleToTitle() {
|
|
|
|
fmt.Println(strings.ToTitle("loud noises"))
|
|
|
|
fmt.Println(strings.ToTitle("хлеб"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output:
|
|
|
|
// LOUD NOISES
|
|
|
|
// ХЛЕБ
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleTrim() {
|
2014-06-05 01:15:33 +02:00
|
|
|
fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
|
|
|
|
// Output: ["Achtung! Achtung"]
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleMap() {
|
|
|
|
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.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleTrimSpace() {
|
|
|
|
fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: a lone gopher
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewReplacer() {
|
|
|
|
r := strings.NewReplacer("<", "<", ">", ">")
|
|
|
|
fmt.Println(r.Replace("This is <b>HTML</b>!"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: This is <b>HTML</b>!
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleToUpper() {
|
|
|
|
fmt.Println(strings.ToUpper("Gopher"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: GOPHER
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleToLower() {
|
|
|
|
fmt.Println(strings.ToLower("Gopher"))
|
2012-03-02 21:01:37 +01:00
|
|
|
// Output: gopher
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
func ExampleTrimSuffix() {
|
|
|
|
var s = "Hello, goodbye, etc!"
|
|
|
|
s = strings.TrimSuffix(s, "goodbye, etc!")
|
|
|
|
s = strings.TrimSuffix(s, "planet")
|
|
|
|
fmt.Print(s, "world!")
|
|
|
|
// Output: Hello, world!
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleTrimPrefix() {
|
|
|
|
var s = "Goodbye,, world!"
|
|
|
|
s = strings.TrimPrefix(s, "Goodbye,")
|
|
|
|
s = strings.TrimPrefix(s, "Howdy,")
|
|
|
|
fmt.Print("Hello" + s)
|
|
|
|
// Output: Hello, world!
|
|
|
|
}
|