golang strings包
zb_liang
· · 2937 次点击 ·
·
开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
010 |
fmt.Println( "查找子串是否在指定的字符串中" ) |
011 |
fmt.Println( " Contains 函数的用法" ) |
012 |
fmt.Println(strings.Contains( "seafood" ,
"foo" )) |
013 |
fmt.Println(strings.Contains( "seafood" ,
"bar" )) |
014 |
fmt.Println(strings.Contains( "seafood" ,
"" )) |
015 |
fmt.Println(strings.Contains( "" ,
"" )) |
016 |
fmt.Println(strings.Contains( "我是中国人" ,
"我" )) |
019 |
fmt.Println( " ContainsAny 函数的用法" ) |
020 |
fmt.Println(strings.ContainsAny( "team" ,
"i" )) |
021 |
fmt.Println(strings.ContainsAny( "failure" ,
"u & i" )) |
022 |
fmt.Println(strings.ContainsAny( "foo" ,
"" )) |
023 |
fmt.Println(strings.ContainsAny( "" ,
"" )) |
026 |
fmt.Println( " ContainsRune 函数的用法" ) |
027 |
fmt.Println(strings.ContainsRune( "我是中国" ,
'我' )) |
030 |
fmt.Println( " Count 函数的用法" ) |
031 |
fmt.Println(strings.Count( "cheese" ,
"e" )) |
032 |
fmt.Println(strings.Count( "five" ,
"" )) |
035 |
fmt.Println( " EqualFold 函数的用法" ) |
036 |
fmt.Println(strings.EqualFold( "Go" ,
"go" )) |
039 |
fmt.Println( " Fields 函数的用法" ) |
040 |
fmt.Println( "Fields are: %q" , strings.Fields( " foo bar baz " ))
|
043 |
for
_, record := range []string{ " aaa*1892*122" ,
"aaa\taa\t" , "124|939|22" } { |
044 |
fmt.Println(strings.FieldsFunc(record, func(ch rune)
bool { |
054 |
fmt.Println( " HasPrefix 函数的用法" ) |
055 |
fmt.Println(strings.HasPrefix( "NLT_abc" ,
"NLT" )) |
058 |
fmt.Println( " HasSuffix 函数的用法" ) |
059 |
fmt.Println(strings.HasSuffix( "NLT_abc" ,
"abc" )) |
062 |
fmt.Println( " Index 函数的用法" ) |
063 |
fmt.Println(strings.Index( "NLT_abc" ,
"abc" )) |
064 |
fmt.Println(strings.Index( "NLT_abc" ,
"aaa" )) |
065 |
fmt.Println(strings.Index( "我是中国人" ,
"中" )) |
068 |
fmt.Println( " IndexAny 函数的用法" ) |
069 |
fmt.Println(strings.IndexAny( "我是中国人" ,
"中" )) |
070 |
fmt.Println(strings.IndexAny( "我是中国人" ,
"和" )) |
073 |
fmt.Println( " Index 函数的用法" ) |
074 |
fmt.Println(strings.IndexRune( "NLT_abc" ,
'b' )) |
075 |
fmt.Println(strings.IndexRune( "NLT_abc" ,
's' )) |
076 |
fmt.Println(strings.IndexRune( "我是中国人" ,
'中' )) |
079 |
fmt.Println( " Join 函数的用法" ) |
080 |
s := []string{ "foo" ,
"bar" , "baz" } |
081 |
fmt.Println(strings.Join(s,
", " )) |
084 |
fmt.Println( " LastIndex 函数的用法" ) |
085 |
fmt.Println(strings.LastIndex( "go gopher" ,
"go" )) |
088 |
fmt.Println( " LastIndexAny 函数的用法" ) |
089 |
fmt.Println(strings.LastIndexAny( "go gopher" ,
"go" )) |
090 |
fmt.Println(strings.LastIndexAny( "我是中国人" ,
"中" )) |
093 |
fmt.Println( " Map 函数的用法" ) |
094 |
rot13 := func(r rune) rune { |
096 |
case
r >= 'A'
&& r <= 'Z' : |
097 |
return
'A' + (r- 'A' +13)%26 |
098 |
case
r >= 'a'
&& r <= 'z' : |
099 |
return
'a' + (r- 'a' +13)%26 |
103 |
fmt.Println(strings.Map(rot13,
"'Twas brillig and the slithy gopher..." )) |
106 |
fmt.Println( " Repeat 函数的用法" ) |
107 |
fmt.Println( "ba"
+ strings.Repeat( "na" , 2))
|
110 |
fmt.Println( " Replace 函数的用法" ) |
111 |
fmt.Println(strings.Replace( "oink oink oink" ,
"k" , "ky" , 2)) |
112 |
fmt.Println(strings.Replace( "oink oink oink" ,
"oink" , "moo" , -1)) |
115 |
fmt.Println( " Split 函数的用法" ) |
116 |
fmt.Printf( "%q\n" , strings.Split( "a,b,c" ,
"," )) |
117 |
fmt.Printf( "%q\n" , strings.Split( "a man a plan a canal panama" ,
"a " )) |
118 |
fmt.Printf( "%q\n" , strings.Split( " xyz " ,
"" )) |
119 |
fmt.Printf( "%q\n" , strings.Split( "" ,
"Bernardo O'Higgins" )) |
122 |
fmt.Println( " SplitAfter 函数的用法" ) |
123 |
fmt.Printf( "%q\n" , strings.SplitAfter( "/home/m_ta/src" ,
"/" )) |
126 |
fmt.Println( " SplitAfterN 函数的用法" ) |
127 |
fmt.Printf( "%q\n" , strings.SplitAfterN( "/home/m_ta/src" ,
"/" , 2)) |
128 |
fmt.Printf( "%q\n" , strings.SplitAfterN( "#home#m_ta#src" ,
"#" , -1)) |
131 |
fmt.Println( " SplitN 函数的用法" ) |
132 |
fmt.Printf( "%q\n" , strings.SplitN( "/home/m_ta/src" ,
"/" , 1)) |
134 |
fmt.Printf( "%q\n" , strings.SplitN( "/home/m_ta/src" ,
"/" , 2)) |
135 |
fmt.Printf( "%q\n" , strings.SplitN( "/home/m_ta/src" ,
"/" , -1)) |
136 |
fmt.Printf( "%q\n" , strings.SplitN( "home,m_ta,src" ,
"," , 2)) |
138 |
fmt.Printf( "%q\n" , strings.SplitN( "#home#m_ta#src" ,
"#" , -1)) |
141 |
fmt.Println( " Title 函数的用法" )
|
142 |
fmt.Println(strings.Title( "her royal highness" )) |
145 |
fmt.Println( " ToLower 函数的用法" ) |
146 |
fmt.Println(strings.ToLower( "Gopher" ))
|
149 |
fmt.Println( " ToLowerSpecial 函数的用法" ) |
152 |
fmt.Println( " ToTitle 函数的用法" ) |
153 |
fmt.Println(strings.ToTitle( "loud noises" )) |
154 |
fmt.Println(strings.ToTitle( "loud 中国" )) |
157 |
fmt.Println( " Replace 函数的用法" ) |
158 |
fmt.Println(strings.Replace( "ABAACEDF" ,
"A" , "a" , 2))
|
160 |
fmt.Println(strings.Replace( "ABAACEDF" ,
"A" , "a" , -1))
|
163 |
fmt.Println( " ToUpper 函数的用法" ) |
164 |
fmt.Println(strings.ToUpper( "Gopher" ))
|
167 |
fmt.Println( " Trim 函数的用法" ) |
168 |
fmt.Printf( "[%q]" , strings.Trim( " !!! Achtung !!! " ,
"! " )) |
171 |
fmt.Println( " TrimLeft 函数的用法" ) |
172 |
fmt.Printf( "[%q]" , strings.TrimLeft( " !!! Achtung !!! " ,
"! " )) |
175 |
fmt.Println( " TrimSpace 函数的用法" ) |
176 |
fmt.Println(strings.TrimSpace( " \t\n a lone gopher \n\t\r\n" ))
|
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889