example_test.go 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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"
10
	"unicode"
11 12 13 14
)

func ExampleFields() {
	fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
15
	// Output: Fields are: ["foo" "bar" "baz"]
16
}
17

18 19 20 21 22 23 24 25
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"]
}

26 27 28 29 30
func ExampleContains() {
	fmt.Println(strings.Contains("seafood", "foo"))
	fmt.Println(strings.Contains("seafood", "bar"))
	fmt.Println(strings.Contains("seafood", ""))
	fmt.Println(strings.Contains("", ""))
31 32 33 34 35
	// Output:
	// true
	// false
	// true
	// true
36 37 38 39 40 41 42
}

func ExampleContainsAny() {
	fmt.Println(strings.ContainsAny("team", "i"))
	fmt.Println(strings.ContainsAny("failure", "u & i"))
	fmt.Println(strings.ContainsAny("foo", ""))
	fmt.Println(strings.ContainsAny("", ""))
43 44 45 46 47
	// Output:
	// false
	// true
	// false
	// false
48 49 50 51 52
}

func ExampleCount() {
	fmt.Println(strings.Count("cheese", "e"))
	fmt.Println(strings.Count("five", "")) // before & after each rune
53 54 55
	// Output:
	// 3
	// 5
56 57 58 59
}

func ExampleEqualFold() {
	fmt.Println(strings.EqualFold("Go", "go"))
60
	// Output: true
61 62 63 64 65
}

func ExampleIndex() {
	fmt.Println(strings.Index("chicken", "ken"))
	fmt.Println(strings.Index("chicken", "dmr"))
66 67 68
	// Output:
	// 4
	// -1
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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
}

90
func ExampleIndexRune() {
91 92
	fmt.Println(strings.IndexRune("chicken", 'k'))
	fmt.Println(strings.IndexRune("chicken", 'd'))
93 94 95
	// Output:
	// 4
	// -1
96 97 98 99 100 101
}

func ExampleLastIndex() {
	fmt.Println(strings.Index("go gopher", "go"))
	fmt.Println(strings.LastIndex("go gopher", "go"))
	fmt.Println(strings.LastIndex("go gopher", "rodent"))
102 103 104 105
	// Output:
	// 0
	// 3
	// -1
106 107 108 109 110
}

func ExampleJoin() {
	s := []string{"foo", "bar", "baz"}
	fmt.Println(strings.Join(s, ", "))
111
	// Output: foo, bar, baz
112 113 114 115
}

func ExampleRepeat() {
	fmt.Println("ba" + strings.Repeat("na", 2))
116
	// Output: banana
117 118 119 120 121
}

func ExampleReplace() {
	fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
	fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
122 123 124
	// Output:
	// oinky oinky oink
	// moo moo moo
125 126 127 128 129 130 131
}

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"))
132 133 134 135 136
	// Output:
	// ["a" "b" "c"]
	// ["" "man " "plan " "canal panama"]
	// [" " "x" "y" "z" " "]
	// [""]
137 138 139 140 141 142
}

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)
143 144 145
	// Output:
	// ["a" "b,c"]
	// [] (nil = true)
146 147 148 149
}

func ExampleSplitAfter() {
	fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
150
	// Output: ["a," "b," "c"]
151 152 153 154
}

func ExampleSplitAfterN() {
	fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
155
	// Output: ["a," "b,c"]
156 157 158 159
}

func ExampleTitle() {
	fmt.Println(strings.Title("her royal highness"))
160
	// Output: Her Royal Highness
161 162 163 164 165
}

func ExampleToTitle() {
	fmt.Println(strings.ToTitle("loud noises"))
	fmt.Println(strings.ToTitle("хлеб"))
166 167 168
	// Output:
	// LOUD NOISES
	// ХЛЕБ
169 170 171
}

func ExampleTrim() {
172 173
	fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
	// Output: ["Achtung! Achtung"]
174 175 176 177 178 179 180 181 182 183 184 185 186
}

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..."))
187
	// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
188 189 190 191
}

func ExampleTrimSpace() {
	fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
192
	// Output: a lone gopher
193 194 195 196 197
}

func ExampleNewReplacer() {
	r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
	fmt.Println(r.Replace("This is <b>HTML</b>!"))
198
	// Output: This is &lt;b&gt;HTML&lt;/b&gt;!
199 200 201 202
}

func ExampleToUpper() {
	fmt.Println(strings.ToUpper("Gopher"))
203
	// Output: GOPHER
204 205 206 207
}

func ExampleToLower() {
	fmt.Println(strings.ToLower("Gopher"))
208
	// Output: gopher
209
}
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

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!
}