| | |
| | |
| | |
| |
|
| | package strings_test |
| |
|
| | import ( |
| | "fmt" |
| | "strings" |
| | "unicode" |
| | "unsafe" |
| | ) |
| |
|
| | func ExampleClone() { |
| | s := "abc" |
| | clone := strings.Clone(s) |
| | fmt.Println(s == clone) |
| | fmt.Println(unsafe.StringData(s) == unsafe.StringData(clone)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBuilder() { |
| | var b strings.Builder |
| | for i := 3; i >= 1; i-- { |
| | fmt.Fprintf(&b, "%d...", i) |
| | } |
| | b.WriteString("ignition") |
| | fmt.Println(b.String()) |
| |
|
| | |
| | } |
| |
|
| | func ExampleCompare() { |
| | fmt.Println(strings.Compare("a", "b")) |
| | fmt.Println(strings.Compare("a", "a")) |
| | fmt.Println(strings.Compare("b", "a")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContains() { |
| | fmt.Println(strings.Contains("seafood", "foo")) |
| | fmt.Println(strings.Contains("seafood", "bar")) |
| | fmt.Println(strings.Contains("seafood", "")) |
| | fmt.Println(strings.Contains("", "")) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsAny() { |
| | fmt.Println(strings.ContainsAny("team", "i")) |
| | fmt.Println(strings.ContainsAny("fail", "ui")) |
| | fmt.Println(strings.ContainsAny("ure", "ui")) |
| | fmt.Println(strings.ContainsAny("failure", "ui")) |
| | fmt.Println(strings.ContainsAny("foo", "")) |
| | fmt.Println(strings.ContainsAny("", "")) |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsRune() { |
| | |
| | |
| | fmt.Println(strings.ContainsRune("aardvark", 97)) |
| | fmt.Println(strings.ContainsRune("timeout", 97)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsFunc() { |
| | f := func(r rune) bool { |
| | return r == 'a' || r == 'e' || r == 'i' || r == 'o' || r == 'u' |
| | } |
| | fmt.Println(strings.ContainsFunc("hello", f)) |
| | fmt.Println(strings.ContainsFunc("rhythms", f)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCount() { |
| | fmt.Println(strings.Count("cheese", "e")) |
| | fmt.Println(strings.Count("five", "")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCut() { |
| | show := func(s, sep string) { |
| | before, after, found := strings.Cut(s, sep) |
| | fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "ph") |
| | show("Gopher", "er") |
| | show("Gopher", "Badger") |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCutPrefix() { |
| | show := func(s, prefix string) { |
| | after, found := strings.CutPrefix(s, prefix) |
| | fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, prefix, after, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "ph") |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCutSuffix() { |
| | show := func(s, suffix string) { |
| | before, found := strings.CutSuffix(s, suffix) |
| | fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, suffix, before, found) |
| | } |
| | show("Gopher", "Go") |
| | show("Gopher", "er") |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleEqualFold() { |
| | fmt.Println(strings.EqualFold("Go", "go")) |
| | fmt.Println(strings.EqualFold("AB", "ab")) |
| | fmt.Println(strings.EqualFold("ß", "ss")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleFields() { |
| | fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) |
| | |
| | } |
| |
|
| | 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)) |
| | |
| | } |
| |
|
| | func ExampleHasPrefix() { |
| | fmt.Println(strings.HasPrefix("Gopher", "Go")) |
| | fmt.Println(strings.HasPrefix("Gopher", "C")) |
| | fmt.Println(strings.HasPrefix("Gopher", "")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | 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", "")) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndex() { |
| | fmt.Println(strings.Index("chicken", "ken")) |
| | fmt.Println(strings.Index("chicken", "dmr")) |
| | |
| | |
| | |
| | } |
| |
|
| | 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)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexAny() { |
| | fmt.Println(strings.IndexAny("chicken", "aeiouy")) |
| | fmt.Println(strings.IndexAny("crwth", "aeiouy")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexByte() { |
| | fmt.Println(strings.IndexByte("golang", 'g')) |
| | fmt.Println(strings.IndexByte("gophers", 'h')) |
| | fmt.Println(strings.IndexByte("golang", 'x')) |
| | |
| | |
| | |
| | |
| | } |
| | func ExampleIndexRune() { |
| | fmt.Println(strings.IndexRune("chicken", 'k')) |
| | fmt.Println(strings.IndexRune("chicken", 'd')) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndex() { |
| | fmt.Println(strings.Index("go gopher", "go")) |
| | fmt.Println(strings.LastIndex("go gopher", "go")) |
| | fmt.Println(strings.LastIndex("go gopher", "rodent")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexAny() { |
| | fmt.Println(strings.LastIndexAny("go gopher", "go")) |
| | fmt.Println(strings.LastIndexAny("go gopher", "rodent")) |
| | fmt.Println(strings.LastIndexAny("go gopher", "fail")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexByte() { |
| | fmt.Println(strings.LastIndexByte("Hello, world", 'l')) |
| | fmt.Println(strings.LastIndexByte("Hello, world", 'o')) |
| | fmt.Println(strings.LastIndexByte("Hello, world", 'x')) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLastIndexFunc() { |
| | fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber)) |
| | fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber)) |
| | fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleJoin() { |
| | s := []string{"foo", "bar", "baz"} |
| | fmt.Println(strings.Join(s, ", ")) |
| | |
| | } |
| |
|
| | func ExampleRepeat() { |
| | fmt.Println("ba" + strings.Repeat("na", 2)) |
| | |
| | } |
| |
|
| | func ExampleReplace() { |
| | fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) |
| | fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleReplaceAll() { |
| | fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo")) |
| | |
| | |
| | } |
| |
|
| | 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")) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | 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) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitAfter() { |
| | fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) |
| | |
| | } |
| |
|
| | func ExampleSplitAfterN() { |
| | fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) |
| | |
| | } |
| |
|
| | func ExampleTitle() { |
| | |
| | fmt.Println(strings.Title("her royal highness")) |
| | fmt.Println(strings.Title("loud noises")) |
| | fmt.Println(strings.Title("брат")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToTitle() { |
| | |
| | fmt.Println(strings.ToTitle("her royal highness")) |
| | fmt.Println(strings.ToTitle("loud noises")) |
| | fmt.Println(strings.ToTitle("брат")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleToTitleSpecial() { |
| | fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) |
| | |
| | |
| | } |
| |
|
| | 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...")) |
| | |
| | } |
| |
|
| | func ExampleNewReplacer() { |
| | r := strings.NewReplacer("<", "<", ">", ">") |
| | fmt.Println(r.Replace("This is <b>HTML</b>!")) |
| | |
| | } |
| |
|
| | func ExampleToUpper() { |
| | fmt.Println(strings.ToUpper("Gopher")) |
| | |
| | } |
| |
|
| | func ExampleToUpperSpecial() { |
| | fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) |
| | |
| | } |
| |
|
| | func ExampleToLower() { |
| | fmt.Println(strings.ToLower("Gopher")) |
| | |
| | } |
| |
|
| | func ExampleToLowerSpecial() { |
| | fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Örnek İş")) |
| | |
| | } |
| |
|
| | func ExampleTrim() { |
| | fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) |
| | |
| | } |
| |
|
| | func ExampleTrimSpace() { |
| | fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) |
| | |
| | } |
| |
|
| | func ExampleTrimPrefix() { |
| | var s = "¡¡¡Hello, Gophers!!!" |
| | s = strings.TrimPrefix(s, "¡¡¡Hello, ") |
| | s = strings.TrimPrefix(s, "¡¡¡Howdy, ") |
| | fmt.Print(s) |
| | |
| | } |
| |
|
| | func ExampleTrimSuffix() { |
| | var s = "¡¡¡Hello, Gophers!!!" |
| | s = strings.TrimSuffix(s, ", Gophers!!!") |
| | s = strings.TrimSuffix(s, ", Marmots!!!") |
| | fmt.Print(s) |
| | |
| | } |
| |
|
| | func ExampleTrimFunc() { |
| | fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { |
| | return !unicode.IsLetter(r) && !unicode.IsNumber(r) |
| | })) |
| | |
| | } |
| |
|
| | func ExampleTrimLeft() { |
| | fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) |
| | |
| | } |
| |
|
| | func ExampleTrimLeftFunc() { |
| | fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { |
| | return !unicode.IsLetter(r) && !unicode.IsNumber(r) |
| | })) |
| | |
| | } |
| |
|
| | func ExampleTrimRight() { |
| | fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) |
| | |
| | } |
| |
|
| | func ExampleTrimRightFunc() { |
| | fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { |
| | return !unicode.IsLetter(r) && !unicode.IsNumber(r) |
| | })) |
| | |
| | } |
| |
|
| | func ExampleToValidUTF8() { |
| | fmt.Printf("%s\n", strings.ToValidUTF8("abc", "\uFFFD")) |
| | fmt.Printf("%s\n", strings.ToValidUTF8("a\xffb\xC0\xAFc\xff", "")) |
| | fmt.Printf("%s\n", strings.ToValidUTF8("\xed\xa0\x80", "abc")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleLines() { |
| | text := "Hello\nWorld\nGo Programming\n" |
| | for line := range strings.Lines(text) { |
| | fmt.Printf("%q\n", line) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitSeq() { |
| | s := "a,b,c,d" |
| | for part := range strings.SplitSeq(s, ",") { |
| | fmt.Printf("%q\n", part) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSplitAfterSeq() { |
| | s := "a,b,c,d" |
| | for part := range strings.SplitAfterSeq(s, ",") { |
| | fmt.Printf("%q\n", part) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleFieldsSeq() { |
| | text := "The quick brown fox" |
| | fmt.Println("Split string into fields:") |
| | for word := range strings.FieldsSeq(text) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | textWithSpaces := " lots of spaces " |
| | fmt.Println("\nSplit string with multiple spaces:") |
| | for word := range strings.FieldsSeq(textWithSpaces) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleFieldsFuncSeq() { |
| | text := "The quick brown fox" |
| | fmt.Println("Split on whitespace(similar to FieldsSeq):") |
| | for word := range strings.FieldsFuncSeq(text, unicode.IsSpace) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | mixedText := "abc123def456ghi" |
| | fmt.Println("\nSplit on digits:") |
| | for word := range strings.FieldsFuncSeq(mixedText, unicode.IsDigit) { |
| | fmt.Printf("%q\n", word) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|