3-3 string包
作者:互联网
func main() { //检索子串 //是否包含子串 返回true或false fmt.Println(strings.Contains("hello", "el")) fmt.Println(strings.Contains("hello", "anso")) //包含任意一个字符 fmt.Println(strings.ContainsAny("hello", "asshole")) fmt.Println(strings.ContainsAny("hello", "ass")) //以字符集序号和字符形式显示 fmt.Printf("%U\n", 'h') fmt.Printf("%c\n", 0x6211) //判断是否包含任意字符 fmt.Println(strings.ContainsRune("hello", 'h')) //返回后面的字符串在前面字符串所属的位置 从序号0开始,没有返回-1 fmt.Println(strings.Index("fuckoff","fuck")) fmt.Println(strings.Index("fuckoff","off")) fmt.Println(strings.Index("fuckoff","shit")) //返回后面任一字符在前面字符串所在的位置 fmt.Println(strings.IndexAny("fuckoff","ashole")) fmt.Println(strings.IndexRune("fuckoff",0x6211)) fmt.Println(strings.IndexRune("fuck我off",0x6211))
}
格式化和裁剪
func main() { //字符串格式化 fmt.Println(strings.ToUpper("Hello")) //HELLO 转换为大写 fmt.Println(strings.ToLower("Hello")) //hello 转换为小写 fmt.Println(strings.Title("hello")) //Hello 首字母大写 //比较大写 fmt.Println(strings.Compare("mfuck","zshit")) //后面大返回-1 fmt.Println(strings.Compare("mfuck","ashit")) //前面大返回1 fmt.Println(strings.Compare("mfuck","mfuck")) //一样大返回0,如果前面的字符相同,会比较后面字符的大小 //裁剪 //去掉头尾的空格 fmt.Println(strings.TrimSpace(" fuck you fuck me ")) //去掉前缀 fmt.Println(strings.TrimPrefix("和: fuckyoufuckme","和: ")) //去掉后缀 fmt.Println(strings.TrimSuffix("和: fuckyoufuckme ohhh"," ohhh")) //在s中 去掉cutset包含的字符 fmt.Println(strings.Trim("fuckyoufuckme","fuck")) //去掉左边的fuck字符 fmt.Println(strings.TrimLeft("fuckyoufuckme","fuck")) //去掉右边的fuck字符 fmt.Println(strings.TrimRight("fuckyoufuckme","fuck")) //使用trimfunc自定义裁剪规则 fmt.Println(strings.TrimFunc("fuckyoufuckme",filter)) //封装为匿名函数 fmt.Println(strings.TrimFunc("fuckyoufuckme", func(char rune) bool { if char == 'f' ||char =='u'{ return true }else { return false } })) } //如果字符等于f或u 返回true,裁剪掉 func filter(char rune)bool { if char == 'f'|| char =='u'{ return true }else { return false } }
分割和拼接
//切割 func main() { //以空格为分隔符 strs := strings.Split("fuck you fuck me", " ") fmt.Println(len(strs),strs) //切割为N段,N=-1时有多碎炸多碎。 strs = strings.SplitN("fuck you fuck me", " ", -1) for _ ,s := range strs{ fmt.Println(s) } //SplitAfter 将分隔符自己也带进去 strs = strings.SplitAfter("fuck, you, fuck, me", ",") fmt.Println(strs) strs = strings.SplitAfterN("fuck, you, fuck, me", ",",2) fmt.Println(strs) //拼接 bigStr := strings.Join([]string{"fuck", "sep", "asshoe"}, "❥(^_-)") fmt.Println(bigStr) }
执行结果
标签:字符,string,strs,fmt,fuck,Println,strings 来源: https://www.cnblogs.com/paad/p/11060620.html