Home | History | Annotate | Download | only in strings
      1 // Copyright 2012 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package strings_test
      6 
      7 import (
      8 	"fmt"
      9 	"strings"
     10 	"unicode"
     11 )
     12 
     13 func ExampleFields() {
     14 	fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
     15 	// Output: Fields are: ["foo" "bar" "baz"]
     16 }
     17 
     18 func ExampleFieldsFunc() {
     19 	f := func(c rune) bool {
     20 		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
     21 	}
     22 	fmt.Printf("Fields are: %q", strings.FieldsFunc("  foo1;bar2,baz3...", f))
     23 	// Output: Fields are: ["foo1" "bar2" "baz3"]
     24 }
     25 
     26 func ExampleContains() {
     27 	fmt.Println(strings.Contains("seafood", "foo"))
     28 	fmt.Println(strings.Contains("seafood", "bar"))
     29 	fmt.Println(strings.Contains("seafood", ""))
     30 	fmt.Println(strings.Contains("", ""))
     31 	// Output:
     32 	// true
     33 	// false
     34 	// true
     35 	// true
     36 }
     37 
     38 func ExampleContainsAny() {
     39 	fmt.Println(strings.ContainsAny("team", "i"))
     40 	fmt.Println(strings.ContainsAny("failure", "u & i"))
     41 	fmt.Println(strings.ContainsAny("foo", ""))
     42 	fmt.Println(strings.ContainsAny("", ""))
     43 	// Output:
     44 	// false
     45 	// true
     46 	// false
     47 	// false
     48 }
     49 
     50 func ExampleCount() {
     51 	fmt.Println(strings.Count("cheese", "e"))
     52 	fmt.Println(strings.Count("five", "")) // before & after each rune
     53 	// Output:
     54 	// 3
     55 	// 5
     56 }
     57 
     58 func ExampleEqualFold() {
     59 	fmt.Println(strings.EqualFold("Go", "go"))
     60 	// Output: true
     61 }
     62 
     63 func ExampleIndex() {
     64 	fmt.Println(strings.Index("chicken", "ken"))
     65 	fmt.Println(strings.Index("chicken", "dmr"))
     66 	// Output:
     67 	// 4
     68 	// -1
     69 }
     70 
     71 func ExampleIndexFunc() {
     72 	f := func(c rune) bool {
     73 		return unicode.Is(unicode.Han, c)
     74 	}
     75 	fmt.Println(strings.IndexFunc("Hello, ", f))
     76 	fmt.Println(strings.IndexFunc("Hello, world", f))
     77 	// Output:
     78 	// 7
     79 	// -1
     80 }
     81 
     82 func ExampleIndexAny() {
     83 	fmt.Println(strings.IndexAny("chicken", "aeiouy"))
     84 	fmt.Println(strings.IndexAny("crwth", "aeiouy"))
     85 	// Output:
     86 	// 2
     87 	// -1
     88 }
     89 
     90 func ExampleIndexRune() {
     91 	fmt.Println(strings.IndexRune("chicken", 'k'))
     92 	fmt.Println(strings.IndexRune("chicken", 'd'))
     93 	// Output:
     94 	// 4
     95 	// -1
     96 }
     97 
     98 func ExampleLastIndex() {
     99 	fmt.Println(strings.Index("go gopher", "go"))
    100 	fmt.Println(strings.LastIndex("go gopher", "go"))
    101 	fmt.Println(strings.LastIndex("go gopher", "rodent"))
    102 	// Output:
    103 	// 0
    104 	// 3
    105 	// -1
    106 }
    107 
    108 func ExampleJoin() {
    109 	s := []string{"foo", "bar", "baz"}
    110 	fmt.Println(strings.Join(s, ", "))
    111 	// Output: foo, bar, baz
    112 }
    113 
    114 func ExampleRepeat() {
    115 	fmt.Println("ba" + strings.Repeat("na", 2))
    116 	// Output: banana
    117 }
    118 
    119 func ExampleReplace() {
    120 	fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
    121 	fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
    122 	// Output:
    123 	// oinky oinky oink
    124 	// moo moo moo
    125 }
    126 
    127 func ExampleSplit() {
    128 	fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    129 	fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    130 	fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    131 	fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
    132 	// Output:
    133 	// ["a" "b" "c"]
    134 	// ["" "man " "plan " "canal panama"]
    135 	// [" " "x" "y" "z" " "]
    136 	// [""]
    137 }
    138 
    139 func ExampleSplitN() {
    140 	fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
    141 	z := strings.SplitN("a,b,c", ",", 0)
    142 	fmt.Printf("%q (nil = %v)\n", z, z == nil)
    143 	// Output:
    144 	// ["a" "b,c"]
    145 	// [] (nil = true)
    146 }
    147 
    148 func ExampleSplitAfter() {
    149 	fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
    150 	// Output: ["a," "b," "c"]
    151 }
    152 
    153 func ExampleSplitAfterN() {
    154 	fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
    155 	// Output: ["a," "b,c"]
    156 }
    157 
    158 func ExampleTitle() {
    159 	fmt.Println(strings.Title("her royal highness"))
    160 	// Output: Her Royal Highness
    161 }
    162 
    163 func ExampleToTitle() {
    164 	fmt.Println(strings.ToTitle("loud noises"))
    165 	fmt.Println(strings.ToTitle(""))
    166 	// Output:
    167 	// LOUD NOISES
    168 	// 
    169 }
    170 
    171 func ExampleTrim() {
    172 	fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
    173 	// Output: ["Achtung! Achtung"]
    174 }
    175 
    176 func ExampleMap() {
    177 	rot13 := func(r rune) rune {
    178 		switch {
    179 		case r >= 'A' && r <= 'Z':
    180 			return 'A' + (r-'A'+13)%26
    181 		case r >= 'a' && r <= 'z':
    182 			return 'a' + (r-'a'+13)%26
    183 		}
    184 		return r
    185 	}
    186 	fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
    187 	// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
    188 }
    189 
    190 func ExampleTrimSpace() {
    191 	fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
    192 	// Output: a lone gopher
    193 }
    194 
    195 func ExampleNewReplacer() {
    196 	r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
    197 	fmt.Println(r.Replace("This is <b>HTML</b>!"))
    198 	// Output: This is &lt;b&gt;HTML&lt;/b&gt;!
    199 }
    200 
    201 func ExampleToUpper() {
    202 	fmt.Println(strings.ToUpper("Gopher"))
    203 	// Output: GOPHER
    204 }
    205 
    206 func ExampleToLower() {
    207 	fmt.Println(strings.ToLower("Gopher"))
    208 	// Output: gopher
    209 }
    210 
    211 func ExampleTrimSuffix() {
    212 	var s = "Hello, goodbye, etc!"
    213 	s = strings.TrimSuffix(s, "goodbye, etc!")
    214 	s = strings.TrimSuffix(s, "planet")
    215 	fmt.Print(s, "world!")
    216 	// Output: Hello, world!
    217 }
    218 
    219 func ExampleTrimPrefix() {
    220 	var s = "Goodbye,, world!"
    221 	s = strings.TrimPrefix(s, "Goodbye,")
    222 	s = strings.TrimPrefix(s, "Howdy,")
    223 	fmt.Print("Hello" + s)
    224 	// Output: Hello, world!
    225 }
    226