golang語言漸入佳境[22]-string檢索類函式
string檢索類函式
package main import ( "fmt" "strings" "unicode" ) /* 1、func Contains(s, substr string) bool 判斷字串s是否包含substr字串 2、func ContainsAny(s, chars string) bool 判斷字串s是否包含chars字串中的任一字元 3、func ContainsRune(s string, r rune) bool 判斷字串s是否包含unicode碼值r 4、func Count(s, sep string) int 返回字串s包含字串sep的個數 5、func HasPrefix(s, prefix string) bool 判斷字串s是否有字首字串prefix 6、func HasSuffix(s, suffix string) bool 判斷字串s是否有後綴字串suffix 7、func Index(s, sep string) int 返回字串s中字串sep首次出現的位置 8、func IndexAny(s, chars string) int 返回字串chars中的任一unicode碼值r在s中首次出現的位置 9、func IndexByte(s string, c byte) int 返回字串s中字元c首次出現位置 10、func IndexFunc(s string, f func(rune) bool) int 返回字串s中滿足函式f(r)==true字元首次出現的位置 11、func IndexRune(s string, r rune) int 返回unicode碼值r在字串中首次出現的位置 12、func LastIndex(s, sep string) int 返回字串s中字串sep最後一次出現的位置 13、func LastIndexAny(s, chars string) int 返回字串s中任意一個unicode碼值r最後一次出現的位置 14、func LastIndexByte(s string, c byte) int 返回字串s中字元c最後一次出現的位置 15、func LastIndexFunc(s string, f func(rune) bool) int 返回字串s中滿足函式f(r)==true字元最後一次出現的位置 */ func main() { TestLastIndexFunc() } func TestContains() { fmt.Println(strings.Contains("seafood", "foo"))//true fmt.Println(strings.Contains("seafood", "bar"))//false fmt.Println(strings.Contains("seafood", ""))//true fmt.Println(strings.Contains("", ""))//true fmt.Println(strings.Contains("jonson鄭2008", "鄭"))//true } func TestContainsAny() { fmt.Println(strings.ContainsAny("team", "i"))//false fmt.Println(strings.ContainsAny("failure", "u & i"))//true fmt.Println(strings.ContainsAny("foo", ""))//false fmt.Println(strings.ContainsAny("", ""))//false } func TestContainsRune() { fmt.Println(strings.ContainsRune("一丁丂", '丁'))//true fmt.Println(strings.ContainsRune("一丁丂", 19969))//true } func TestCount() { fmt.Println(strings.Count("cheese", "e"))//3 fmt.Println(strings.Count("one", ""))//4 } func TestHasPrefix() { fmt.Println(strings.HasPrefix("1000phone news", "1000"))//true fmt.Println(strings.HasPrefix("1000phone news", "1000a"))//false } func TestHasSuffix() { fmt.Println(strings.HasSuffix("1000phone news", "news"))//true fmt.Println(strings.HasSuffix("1000phone news", "new"))//false } func TestIndex() { fmt.Println(strings.Index("chicken", "ken"))//4 fmt.Println(strings.Index("chicken", "dmr"))//-1 } func TestIndexAny() { fmt.Println(strings.IndexAny("abcABC120", "教育基地A"))//3 } func TestIndexByte() { fmt.Println(strings.IndexByte("123abc", 'a'))//3 } func TestIndexRune() { fmt.Println(strings.IndexRune("abcABC120", 'C'))//5 fmt.Println(strings.IndexRune("It培訓教育", '教'))//8 } func TestIndexFunc() { f := func(c rune) bool { return unicode.Is(unicode.Han , c) } fmt.Println(strings.IndexFunc("Hello123,中國" , f))//9 } func TestLastIndex() { fmt.Println(strings.LastIndex("jonson learn english", "e"))//13 fmt.Println(strings.Index("go gopher", "go"))//0 fmt.Println(strings.LastIndex("go gopher", "go"))//3 fmt.Println(strings.LastIndex("go gopher", "rodent"))//-1 } func TestLastIndexAny() { fmt.Println(strings.LastIndexAny("chicken", "aeiouy"))//5 fmt.Println(strings.LastIndexAny("crwth", "aeiouy"))//-1 } func TestLastIndexByte() { fmt.Println(strings.LastIndexByte("abcABCA123", 'A'))//6 } func TestLastIndexFunc() { f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.LastIndexFunc("Hello,世界", f))//9 fmt.Println(strings.LastIndexFunc("Hello,world中國人", f))//17 }
-
本文連結: ofollow,noindex">https://dreamerjonson.com/2018/11/30/golang-22-string-package/
-
版權宣告: 本部落格所有文章除特別宣告外,均採用 CC BY 4.0 CN協議 許可協議。轉載請註明出處!