go库函数之-strings-使用示例

//###########################################################
//D:\go\go\go库源码\源码库测试文件集合\strings-example_test.go

// 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"
   "unicode"
)

func ExampleFields() {
   // 以一个或者多个空格分割成切片
   //strings.Fields("  foo bar  baz   ")
   fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
   // Output: Fields are: ["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))
   // Output: Fields are: ["foo1" "bar2" "baz3"]
}

func ExampleCompare() {
   // 比较a和b, 返回 0: a等于b, 1: a包含b, -1: a不包含b
   //bytes.Compare(a, b)
   fmt.Println(strings.Compare("a", "b"))
   fmt.Println(strings.Compare("a", "a"))
   fmt.Println(strings.Compare("b", "a"))
   // Output:
   // -1
   // 0
   // 1
}

func ExampleContains() {
   // 判断a是否包含b
   //bytes.Contains(a, b)
   fmt.Println(strings.Contains("seafood", "foo"))
   fmt.Println(strings.Contains("seafood", "bar"))
   fmt.Println(strings.Contains("seafood", ""))
   fmt.Println(strings.Contains("", ""))
   // Output:
   // true
   // false
   // true
   // true
}

func ExampleContainsAny() {
   // 判断byte是否包含字符串中任意字符,只要包含字符串中一个及以上字符返回true,否则false
   //bytes.ContainsAny([]byte("I like seafood."), "fÄo!")
   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("", ""))
   // Output:
   // false
   // true
   // true
   // true
   // false
   // false
}

func ExampleContainsRune() {
   // 判断byte是否包含rune字符
   //bytes.ContainsRune([]byte("I like seafood."), 'f')
   // Finds whether a string contains a particular Unicode code point.
   // The code point for the lowercase letter "a", for example, is 97.
   fmt.Println(strings.ContainsRune("aardvark", 97))
   fmt.Println(strings.ContainsRune("timeout", 97))
   // Output:
   // true
   // false
}

func ExampleCount() {
   // 统计a中包含所有b的个数,如果b为空则返回a的长度
   //bytes.Count(a, b)
   fmt.Println(strings.Count("cheese", "e"))
   fmt.Println(strings.Count("five", "")) // before & after each rune
   // Output:
   // 3
   // 5
}

func ExampleEqualFold() {
   // 判断a与b是否相同(将unicode大写、小写、标题三种格式字符视为相同)
   //strings.EqualFold(a, b)
   fmt.Println(strings.EqualFold("Go", "go"))
   // Output: true
}

func ExampleHasPrefix() {
   //判断a是否以b开头,当b为空时true
   //strings.HasPrefix(a, b)
   fmt.Println(strings.HasPrefix("Gopher", "Go"))
   fmt.Println(strings.HasPrefix("Gopher", "C"))
   fmt.Println(strings.HasPrefix("Gopher", ""))
   // Output:
   // true
   // false
   // true
}

func ExampleHasSuffix() {
   //判断a是否以b结尾,当b为空时true
   //strings.HasSuffix(a, b)
   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
}

func ExampleIndex() {
   //检索b在a中第一次出现的位置,未检索到返回-1
   //strings.Index(a, b)
   fmt.Println(strings.Index("chicken", "ken"))
   fmt.Println(strings.Index("chicken", "dmr"))
   // Output:
   // 4
   // -1
}

func ExampleIndexFunc() {
   //自定义方法检索首个字符的位置,未检索到返回-1
   //strings.IndexFunc
   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() {
   //检索a中首个 字符串中任意字符 的位置,未检索到返回-1
   //strings.IndexAny(a, "abc")
   fmt.Println(strings.IndexAny("chicken", "aeiouy"))
   fmt.Println(strings.IndexAny("crwth", "aeiouy"))
   // Output:
   // 2
   // -1
}

func ExampleIndexByte() {
   //检索字符c在s中第一次出现的位置,不存在则返回-1
   //strings.IndexByte(a, byte('k'))
   fmt.Println(strings.IndexByte("golang", 'g'))
   fmt.Println(strings.IndexByte("gophers", 'h'))
   fmt.Println(strings.IndexByte("golang", 'x'))
   // Output:
   // 0
   // 3
   // -1
}
func ExampleIndexRune() {
   //检索a中首个 rune类型字符 的位置,未检索到返回-1
   //strings.IndexRune("chicken", 'k')
   fmt.Println(strings.IndexRune("chicken", 'k'))
   fmt.Println(strings.IndexRune("chicken", 'd'))
   // Output:
   // 4
   // -1
}

func ExampleLastIndex() {
   //检索b在a中第一次出现的位置,未检索到返回-1
   //strings.Index(a, b)
   fmt.Println(strings.Index("go gopher", "go"))
   //检索a中最后个b的位置,未检索到返回-1
   //strings.LastIndex(a, b)
   fmt.Println(strings.LastIndex("go gopher", "go"))
   fmt.Println(strings.LastIndex("go gopher", "rodent"))
   // Output:
   // 0
   // 3
   // -1
}

func ExampleLastIndexAny() {
   //返回字符串str中的任何一个字符在字符串s中最后一次出现的位置。
   //如果找不到或str为空则返回-1
   fmt.Println(strings.LastIndexAny("go gopher", "go"))
   fmt.Println(strings.LastIndexAny("go gopher", "rodent"))
   fmt.Println(strings.LastIndexAny("go gopher", "fail"))
   // Output:
   // 4
   // 8
   // -1
}

func ExampleLastIndexByte() {
   //检索a中最后个 byte类型字符 的位置,未检索到返回-1
   //strings.LastIndexByte(a, byte('k'))
   fmt.Println(strings.LastIndexByte("Hello, world", 'l'))
   fmt.Println(strings.LastIndexByte("Hello, world", 'o'))
   fmt.Println(strings.LastIndexByte("Hello, world", 'x'))
   // Output:
   // 10
   // 8
   // -1
}

func ExampleLastIndexFunc() {
   //自定义方法检索最后个字符的位置,未检索到返回-1
   //strings.LastIndexFunc(a, unicode.IsLetter)
   fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber))
   fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber))
   fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber))
   // Output:
   // 5
   // 2
   // -1
}

func ExampleJoin() {
   //将string数组以指定字符连接成一个新的string
   //s := []string{a, b}
   //strings.Join(s, ",")
   s := []string{"foo", "bar", "baz"}
   fmt.Println(strings.Join(s, ", "))
   // Output: foo, bar, baz
}

func ExampleRepeat() {
   // 返回count个s串联的字符串
   // 例如:a = "abc",返回 "abcabc"
   //strings.Repeat(a, 2)
   fmt.Println("ba" + strings.Repeat("na", 2))
   // Output: banana
}

func ExampleReplace() {
   //返回一个 将a中的b替换为c 的新string,n为替换个数,-1替换所有,其他为替换的个数
   //strings.Replace(a, b, c, -1)
   fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
   fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
   // Output:
   // oinky oinky oink
   // moo moo moo
}

func ExampleReplaceAll() {
   //返回一个 将a中的b替换为c 的新string,全部替换
   //strings.ReplaceAll(a, b, c)
   fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
   // Output:
   // moo moo moo
}

func ExampleSplit() {
   //将a以指定字符分割成string数组
   //strings.Split(a, b)
   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"))
   // Output:
   // ["a" "b" "c"]
   // ["" "man " "plan " "canal panama"]
   // [" " "x" "y" "z" " "]
   // [""]
}

func ExampleSplitN() {
   //将a以指定字符分割成string数组, n为分割个数,-1分割所有
   //strings.SplitN(a, b, 2)
   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)
   // Output:
   // ["a" "b,c"]
   // [] (nil = true)
}

func ExampleSplitAfter() {
   //将a以指定字符分割成string数组,保留b。
   //strings.SplitAfter(a, b)
   fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
   // Output: ["a," "b," "c"]
}

func ExampleSplitAfterN() {
   //将a以指定字符分割成string数组,保留b。n为分割个数,-1分割所有
   //strings.SplitAfterN(a, b, 2)
   fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
   // Output: ["a," "b,c"]
}

func ExampleTitle() {
   //返回一个 以空格为界限,所有首个字母大写 的标题格式
   //strings.Title(a)
   // Compare this example to the ToTitle example.
   fmt.Println(strings.Title("her royal highness"))
   fmt.Println(strings.Title("loud noises"))
   fmt.Println(strings.Title("хлеб"))
   // Output:
   // Her Royal Highness
   // Loud Noises
   // Хлеб
}

func ExampleToTitle() {
   //返回一个 所有字母大写 的标题格式
   //strings.ToTitle(a)
   // Compare this example to the Title example.
   fmt.Println(strings.ToTitle("her royal highness"))
   fmt.Println(strings.ToTitle("loud noises"))
   fmt.Println(strings.ToTitle("хлеб"))
   // Output:
   // HER ROYAL HIGHNESS
   // LOUD NOISES
   // ХЛЕБ
}

func ExampleToTitleSpecial() {
   //使用指定的映射表将 a 中的所有字符修改为标题格式返回。
   //strings.ToTitleSpecial(unicode.SpecialCase{}, a)
   fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
   // Output:
   // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR
}

func ExampleMap() {
   //遍历a按指定的rune方法处理每个字符
   //strings.Map
   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..."))
   // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
}

func ExampleNewReplacer() {
   // 使用提供的多组old、new字符串对创建并返回一个*strings.Replacer
   // 替换是依次进行的,匹配时不会重叠
   r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
   fmt.Println(r.Replace("This is <b>HTML</b>!"))
   // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
}

func ExampleToUpper() {
   //所有字母大写
   //strings.ToUpper(a)
   fmt.Println(strings.ToUpper("Gopher"))
   // Output: GOPHER
}

func ExampleToUpperSpecial() {
   // 使用指定的映射表将 a 中的所有字符修改为大写格式返回。
   fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
   // Output: ÖRNEK İŞ
}

func ExampleToLower() {
   // 所有字母小写
   fmt.Println(strings.ToLower("Gopher"))
   // Output: gopher
}

func ExampleToLowerSpecial() {
   // 使用指定的映射表将 a 中的所有字符修改为小写格式返回。
   fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
   // Output: önnek iş
}

func ExampleTrim() {
   // 去除开头结尾所有的 指定字符串中的任意字符
   fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
   // Output: Hello, Gophers
}

func ExampleTrimSpace() {
   // 去除开头结尾所有的 空格换行回车缩进
   fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
   // Output: Hello, Gophers
}

func ExampleTrimPrefix() {
   //如果a以b开头,则返回a去掉b开头部分的新byte。如果不是,返回a
   //bytes.TrimPrefix(a, b)
   var s = "¡¡¡Hello, Gophers!!!"
   s = strings.TrimPrefix(s, "¡¡¡Hello, ")
   s = strings.TrimPrefix(s, "¡¡¡Howdy, ")
   fmt.Print(s)
   // Output: Gophers!!!
}

func ExampleTrimSuffix() {
   //如果a以b结尾,则返回a去掉b结尾部分的新string。如果不是,返回a
   //strings.TrimSuffix(a, b)
   var s = "¡¡¡Hello, Gophers!!!"
   s = strings.TrimSuffix(s, ", Gophers!!!")
   s = strings.TrimSuffix(s, ", Marmots!!!")
   fmt.Print(s)
   // Output: ¡¡¡Hello
}

func ExampleTrimFunc() {
   按自定义方法 去除开头结尾所有指定内容
   strings.TrimFunc(a, unicode.IsLetter)
   fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
      return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   }))
   // Output: Hello, Gophers
}

func ExampleTrimLeft() {
   // 去除开头所有的 指定字符串中的任意字符
   fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
   // Output: Hello, Gophers!!!
}

func ExampleTrimLeftFunc() {
   // 按自定义方法 去除开头所有指定内容
   //strings.TrimLeftFunc(a, unicode.IsLetter)
   fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
      return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   }))
   // Output: Hello, Gophers!!!
}

func ExampleTrimRight() {
   // 去除结尾所有的 指定字符串中的任意字符
   fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
   // Output: ¡¡¡Hello, Gophers
}

func ExampleTrimRightFunc() {
   //按自定义方法 去除结尾所有指定内容
   //strings.TrimRightFunc(a, unicode.IsLetter)
   fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
      return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   }))
   // Output: ¡¡¡Hello, Gophers
}

func ExampleBuilder() {
   //用于字符串拼接
   var b strings.Builder
   for i := 3; i >= 1; i-- {
      fmt.Fprintf(&b, "%d...", i)
   }
   b.WriteString("ignition")
   fmt.Println(b.String())

   // Output: 3...2...1...ignition
}

猜你喜欢

转载自blog.csdn.net/liao__ran/article/details/114667691
今日推荐