1.replace
func Replace(s, old, new string, n int) string
s:原来的字符串
old: 要被替换的久的字符串
new string:要替换旧字符串的新字符串
n:表示替换的次数
package main import "strings" import "fmt" func main() { str := "hello world abcd" result:= strings.Replace(str,"world","monday",1) fmt.Println(result); }
输出:
hello monday abcd
2.Count
func Count
func Count(s, sep string) int
s:整个字符串
sep:里面的子字符串
返回: 返回字符串s中有几个不重复的sep子串。
package main import "strings" import "fmt" func main() { str := "hello world abcd" count:=strings.Count(str,"a"); fmt.Println(count) }
输出:
1
3.repeat
func Repeat(s string, count int) string
string :字符串
count:重复几次 字符串
返回count个s串联的字符串。
package main import "strings" import "fmt" func main() { str := "hello world abcd" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) result:=strings.Repeat(str,2); fmt.Println(result); }
输出:
hello world abcdhello world abcd
4.ToLower
func ToLower(s string) string
返回将所有字母都转为对应的小写版本的拷贝。
解释:将字符串全部转为小写
package main import "strings" import "fmt" func main() { str := "HELLO WORLD" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); result:= strings.ToLower(str); fmt.Println(result); }
输出:
hello world
5. toupper
func ToUpper
func ToUpper(s string) string
返回将所有字母都转为对应的大写版本的拷贝。
解释:将字符串全部转为大写
package main import "strings" import "fmt" func main() { str := "hello world " //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); result:= strings.ToUpper(str) fmt.Println(result); }
输出:
HELLO WORLD
6.strings.TrimSpace(str string): 去掉字符串首尾空白字符
package main import "strings" import "fmt" func main() { str := " hello world " //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); result:=strings.TrimSpace(str); fmt.Println(result); }
输出:
hello world
7.
strings.Trim(str string, cut string):去掉字符串首尾cut字符
去掉指定的左右两边的字符串
package main import "strings" import "fmt" func main() { str := "11hello world11" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); result:=strings.Trim(str,"11"); fmt.Println(result); }
输出:
hello world
8. strings.TrimLeft(str string, cut string):去掉字符串首cut字符
去掉左边的指定字符串
package main import "strings" import "fmt" func main() { str := "11hello world11" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); //result:=strings.Trim(str,"11"); //fmt.Println(result); result:=strings.TrimLeft(str,"11") fmt.Println(result); }
输出:
hello world11
9.strings.TrimRight(str string, cut string):去掉字符串首cut字符
去掉右边的指定字符串
package main import ( "fmt" "strings" ) func main() { str := "11hello world11" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); //result:=strings.Trim(str,"11"); //fmt.Println(result); //result:=strings.TrimLeft(str,"11") //fmt.Println(result); result:=strings.TrimRight(str,"11") fmt.Println(result); }
输出:
11hello world
10. strings.Field(str string):返回str空格分隔的所有子串的slice
将字符串切割成数组返回
package main import ( "fmt" "strings" ) func main() { str := "hello world monday" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); //result:=strings.Trim(str,"11"); //fmt.Println(result); //result:=strings.TrimLeft(str,"11") //fmt.Println(result); //result:=strings.TrimRight(str,"11") //fmt.Println(result); splitResult:= strings.Fields(str) for i:=0;i<len(splitResult);i++ { fmt.Println(splitResult[i]); } }
输出:
hello
world
monday
11.strings.Split(str string, split string):返回str split分隔的所有子串的slice
将字符串按照特定的方式分割成子串
按照某个子字符进行切割
package main import ( "fmt" "strings" ) func main() { str := "hello world monday" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); //result:=strings.Trim(str,"11"); //fmt.Println(result); //result:=strings.TrimLeft(str,"11") //fmt.Println(result); //result:=strings.TrimRight(str,"11") //fmt.Println(result); //splitResult:= strings.Fields(str) //for i:=0;i<len(splitResult);i++ { // fmt.Println(splitResult[i]); //} splitResult:= strings.Split(str,"o") for i:=0;i<len(splitResult);i++ { fmt.Println(splitResult[i]); } }
输出:
hell
w
rld m
nday
12.strings.Join(s1 []string, sep string):用sep把s1中的所有元素链接起来
用sep把s1中的所有元素链接起来
package main import ( "fmt" "strings" ) func main() { str := "hello world monday" //result:= strings.Replace(str,"world","monday",1) //fmt.Println(result); //count:=strings.Count(str,"a"); //fmt.Println(count) // result:=strings.Repeat(str,2); //fmt.Println(result); //result:= strings.ToUpper(str) //fmt.Println(result); //result:=strings.TrimSpace(str); // //fmt.Println(result); //result:=strings.Trim(str,"11"); //fmt.Println(result); //result:=strings.TrimLeft(str,"11") //fmt.Println(result); //result:=strings.TrimRight(str,"11") //fmt.Println(result); //splitResult:= strings.Fields(str) //for i:=0;i<len(splitResult);i++ { // fmt.Println(splitResult[i]); //} splitResult:= strings.Split(str,"o") //for i:=0;i<len(splitResult);i++ { // fmt.Println(splitResult[i]); //} str2:=strings.Join(splitResult,"o") fmt.Println(str2); }
输出:hello world monday