前端之家收集整理的这篇文章主要介绍了
Golang 常用字符串处理方法汇总,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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( " 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( " ContainsRune 函数的用法" ) |
fmt.Println(strings.ContainsRune( "我是中国" , '我' )) |
fmt.Println( " Count 函数的用法" ) |
fmt.Println(strings.Count( "cheese" , "e" )) |
fmt.Println(strings.Count( "five" , "" )) |
fmt.Println( " EqualFold 函数的用法" ) |
fmt.Println(strings.EqualFold( "Go" , "go" )) |
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 { |
fmt.Println( " HasPrefix 函数的用法" ) |
fmt.Println(strings.HasPrefix( "NLT_abc" , "NLT" )) |
fmt.Println( " HasSuffix 函数的用法" ) |
fmt.Println(strings.HasSuffix( "NLT_abc" , "abc" )) |
fmt.Println( " Index 函数的用法" ) |
fmt.Println(strings.Index( "NLT_abc" , "abc" )) |
fmt.Println(strings.Index( "NLT_abc" , "aaa" )) |
fmt.Println(strings.Index( "我是中国人" , "中" )) |
fmt.Println( " IndexAny 函数的用法" ) |
fmt.Println(strings.IndexAny( "我是中国人" , "中" )) |
fmt.Println(strings.IndexAny( "我是中国人" , "和" )) |
fmt.Println( " Index 函数的用法" ) |
fmt.Println(strings.IndexRune( "NLT_abc" , 'b' )) |
fmt.Println(strings.IndexRune( "NLT_abc" , 's' )) |
fmt.Println(strings.IndexRune( "我是中国人" , '中' )) |
fmt.Println( " Join 函数的用法" ) |
s := []string{ "foo" , "bar" , "baz" } |
fmt.Println(strings.Join(s, "," )) |
fmt.Println( " LastIndex 函数的用法" ) |
fmt.Println(strings.LastIndex( "go gopher" , "go" )) |
fmt.Println( " LastIndexAny 函数的用法" ) |
fmt.Println(strings.LastIndexAny( "go gopher" , "go" )) |
fmt.Println(strings.LastIndexAny( "我是中国人" , "中" )) |
fmt.Println( " Map 函数的用法" ) |
rot13 := func(r rune) rune { |
case r >= 'A' && r <= 'Z' : |
return 'A' + (r- 'A' +13)%26 |
case r >= 'a' && r <= 'z' : |
return 'a' + (r- 'a' +13)%26 |
fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..." )) |
fmt.Println( " Repeat 函数的用法" ) |
fmt.Println( "ba" + strings.Repeat( "na" ,2)) |
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( " 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( " SplitAfter 函数的用法" ) |
fmt.Printf( "%q\n" ,strings.SplitAfter( "/home/m_ta/src" , "/" )) |
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( " 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.Println( " Title 函数的用法" ) |
fmt.Println(strings.Title( "her royal highness" )) |
fmt.Println( " ToLower 函数的用法" ) |
fmt.Println(strings.ToLower( "Gopher" )) |
fmt.Println( " ToLowerSpecial 函数的用法" ) |
fmt.Println( " ToTitle 函数的用法" ) |
fmt.Println(strings.ToTitle( "loud noises" )) |
fmt.Println(strings.ToTitle( "loud 中国" )) |
fmt.Println( " Replace 函数的用法" ) |
fmt.Println(strings.Replace( "ABAACEDF" , "A" , "a" ,2)) |
fmt.Println(strings.Replace( "ABAACEDF" ,-1)) |
fmt.Println( " ToUpper 函数的用法" ) |
fmt.Println(strings.ToUpper( "Gopher" )) |
fmt.Println( " Trim 函数的用法" ) |
fmt.Printf( "[%q]" ,strings.Trim( " !!! Achtung !!! " , "! " )) |
fmt.Println( " TrimLeft 函数的用法" ) |
fmt.Printf( "[%q]" ,strings.TrimLeft( " !!! Achtung !!! " , "! " )) |
fmt.Println( " TrimSpace 函数的用法" ) |
fmt.Println(strings.TrimSpace( " \t\n a lone gopher \n\t\r\n" )) |
原文链接:https://www.f2er.com/go/190208.html