字符串常用方法
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("查找子串是否在指定的字符串中")
fmt.Println(" Contains 函数的用法")
fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", ""))
fmt.Println(strings.Contains("我是中国人", "我"))
fmt.Println("")
fmt.Println(" ContainsAny 函数的用法")
fmt.Println(strings.ContainsAny("team", "i"))
fmt.Println(strings.ContainsAny("failure", "u & i"))
fmt.Println(strings.ContainsAny("foo", ""))
fmt.Println(strings.ContainsAny("", ""))
fmt.Println("")
fmt.Println(" ContainsRune 函数的用法")
fmt.Println(strings.ContainsRune("我是中国", '我'))
fmt.Println("")
fmt.Println(" Count 函数的用法")
fmt.Println(strings.Count("cheese", "e"))
fmt.Println(strings.Count("five", ""))
fmt.Println("")
fmt.Println(" EqualFold 函数的用法")
fmt.Println(strings.EqualFold("Go", "go"))
fmt.Println("")
fmt.Println(" Fields 函数的用法")
fmt.Println("Fields are: %q", strings.Fields(" foo bar baz "))
for _, record := range []string{" aaa*1892*122", "aaa\taa\t", "124|939|22"} {
fmt.Println(strings.FieldsFunc(record, func(ch rune) bool {
switch {
case ch > '5':
return true
}
return false
}))
}
fmt.Println("")
fmt.Println(" HasPrefix 函数的用法")
fmt.Println(strings.HasPrefix("NLT_abc", "NLT"))
fmt.Println("")
fmt.Println(" HasSuffix 函数的用法")
fmt.Println(strings.HasSuffix("NLT_abc", "abc"))
fmt.Println("")
fmt.Println(" Index 函数的用法")
fmt.Println(strings.Index("NLT_abc", "abc"))
fmt.Println(strings.Index("NLT_abc", "aaa"))
fmt.Println(strings.Index("我是中国人", "中"))
fmt.Println("")
fmt.Println(" IndexAny 函数的用法")
fmt.Println(strings.IndexAny("我是中国人", "中"))
fmt.Println(strings.IndexAny("我是中国人", "和"))
fmt.Println("")
fmt.Println(" Index 函数的用法")
fmt.Println(strings.IndexRune("NLT_abc", 'b'))
fmt.Println(strings.IndexRune("NLT_abc", 's'))
fmt.Println(strings.IndexRune("我是中国人", '中'))
fmt.Println("")
fmt.Println(" Join 函数的用法")
s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
fmt.Println("")
fmt.Println(" LastIndex 函数的用法")
fmt.Println(strings.LastIndex("go gopher", "go"))
fmt.Println("")
fmt.Println(" LastIndexAny 函数的用法")
fmt.Println(strings.LastIndexAny("go gopher", "go"))
fmt.Println(strings.LastIndexAny("我是中国人", "中"))
fmt.Println("")
fmt.Println(" 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..."))
fmt.Println("")
fmt.Println(" Repeat 函数的用法")
fmt.Println("ba" + strings.Repeat("na", 2))
fmt.Println("")
fmt.Println(" Replace 函数的用法")
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
fmt.Println("")
fmt.Println(" Split 函数的用法")
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"))
fmt.Println("")
fmt.Println(" SplitAfter 函数的用法")
fmt.Printf("%q\n", strings.SplitAfter("/home/m_ta/src", "/"))
fmt.Println("")
fmt.Println(" SplitAfterN 函数的用法")
fmt.Printf("%q\n", strings.SplitAfterN("/home/m_ta/src", "/", 2))
fmt.Printf("%q\n", strings.SplitAfterN("#home#m_ta#src", "#", -1))
fmt.Println("")
fmt.Println(" SplitN 函数的用法")
fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", 1))
fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", 2))
fmt.Printf("%q\n", strings.SplitN("/home/m_ta/src", "/", -1))
fmt.Printf("%q\n", strings.SplitN("home,m_ta,src", ",", 2))
fmt.Printf("%q\n", strings.SplitN("#home#m_ta#src", "#", -1))
fmt.Println("")
fmt.Println(" Title 函数的用法")
fmt.Println(strings.Title("her royal highness"))
fmt.Println("")
fmt.Println(" ToLower 函数的用法")
fmt.Println(strings.ToLower("Gopher"))
fmt.Println("")
fmt.Println(" ToLowerSpecial 函数的用法")
fmt.Println("")
fmt.Println(" ToTitle 函数的用法")
fmt.Println(strings.ToTitle("loud noises"))
fmt.Println(strings.ToTitle("loud 中国"))
fmt.Println("")
fmt.Println(" Replace 函数的用法")
fmt.Println(strings.Replace("ABAACEDF", "A", "a", 2))
fmt.Println(strings.Replace("ABAACEDF", "A", "a", -1))
fmt.Println("")
fmt.Println(" ToUpper 函数的用法")
fmt.Println(strings.ToUpper("Gopher"))
fmt.Println("")
fmt.Println(" Trim 函数的用法")
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
fmt.Println("")
fmt.Println(" TrimLeft 函数的用法")
fmt.Printf("[%q]", strings.TrimLeft(" !!! Achtung !!! ", "! "))
fmt.Println("")
fmt.Println(" TrimSpace 函数的用法")
fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
}