来源:未知 时间:2022-07-11 09:48 作者:小飞侠 阅读:次
[导读] Golang String 常用中文API Go strings API 写代码最痛苦的在于想不起该用什么api,不同的语言api有相似的,当然更多的是不同,这里记录strings的API,用于自己参考和理解使用: 几个重要的例子...
Golang String 常用中文API Go strings API写代码最痛苦的在于想不起该用什么api,不同的语言api有相似的,当然更多的是不同,这里记录strings的API,用于自己参考和理解使用: 几个重要的例子,都附上了exp,然后输出 func Compare(a, b string) int, 比较,a==b:0, a<b:-1, a>b:1 func Contains(s, substr string) bool, 包含 func ContainsAny(s, chars string) bool,任何字符有就是 fmt.Println(strings.ContainsAny("team", "i")) fmt.Println(strings.ContainsAny("fail", "ui")) fmt.Println(strings.ContainsAny("ure", "ui")) fmt.Println(strings.ContainsAny("failure", "ui")) fmt.Println(strings.ContainsAny("foo", "")) fmt.Println(strings.ContainsAny("", ""))} func ContainsRune(s string, r rune) bool // Finds whether a string contains a particular Unicode code point. // The code point for the lowercase letter "a", for example, is 97. fmt.Println(strings.ContainsRune("aardvark", 97)) fmt.Println(strings.ContainsRune("timeout", 97)) func Count(s, substr string) int func EqualFold(s, t string) bool,UTF-8,unicode case-floding equals fmt.Println(strings.EqualFold("Go", "go")) [func Fields(s string) ]string, 根据空格分割字符串返回slice fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) [func FieldsFunc(s string, f func(rune) bool) ]string, 根据空格分割字符串返回slice func main() { f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))} func HasPrefix(s, prefix string) bool, func main() { fmt.Println(strings.HasPrefix("Gopher", "Go")) fmt.Println(strings.HasPrefix("Gopher", "C")) fmt.Println(strings.HasPrefix("Gopher", ""))} func HasSuffix(s, suffix string) bool func main() { fmt.Println(strings.HasSuffix("Amigo", "go")) fmt.Println(strings.HasSuffix("Amigo", "O")) fmt.Println(strings.HasSuffix("Amigo", "Ami")) fmt.Println(strings.HasSuffix("Amigo", ""))} func Index(s, substr string) int, 返回索引,不存在-1, java 有 func IndexAny(s, chars string) int, char 第一个,不存在-1 fmt.Println(strings.IndexAny("chicken", "aeiouy")) fmt.Println(strings.IndexAny("crwth", "aeiouy")) func IndexByte(s string, c byte) int func IndexFunc(s string, f func(rune) bool) int, 第一个满足func f的rune f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.IndexFunc("Hello, 世界", f)) fmt.Println(strings.IndexFunc("Hello, world", f))} func IndexRune(s string, r rune) int [func Join(elems ]string, sep string) string, 组合string slice到s, s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) // 省略几个 lastIndexXXX 相关的,类似IndexXXX func Map(mapping func(rune) rune, s string) string, 把string每个字符 map func一下,如果返回负数,直接抛弃 rot13 := func(r rune) rune { switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) func Repeat(s string, count int) string, 返回新的string,n个s func Replace(s, old, new string, n int) string, 把old换成new,换n个,n<0, 全换, 就是ReplaceAll func ReplaceAll(s, old, new string) string [func Split(s, sep string) ]string,切到slice,如果有没有sep,返回slice[0] = s, 如果sep ==空,返回每个字符一个位置的数组,都空,返回空数组 [func SplitAfter(s, sep string) ]string fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) //["a," "b," "c"] 没看出区别split [func SplitAfterN(s, sep string, n int) ]string fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))//["a," "b,c"] [func SplitN(s, sep string, n int) ]string fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))//["a" "b,c"] z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) // [] (nil = true) func Title(s string) string,返回新的string,第一个字符unicode fmt.Println(strings.Title("her royal highness")) //Her Royal Highness fmt.Println(strings.Title("loud noises")) //Loud Noises fmt.Println(strings.Title("хлеб")) //Хлеб func ToLower(s string) string func ToLowerSpecial(c unicode.SpecialCase, s string) string, 根据具体的unicode 编码来lower fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) //önnek iş // 省略几个ToTitle, ToUpper 相关的,无非是首字母大写或者全大写 func Trim(s string, cutset string) string func TrimFunc(s string, f func(rune) bool) string fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) func TrimLeft(s string, cutset string) string fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) //Hello, Gophers!!! func TrimPrefix(s, prefix string) string var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimPrefix(s, "¡¡¡Hello, ") s = strings.TrimPrefix(s, "¡¡¡Howdy, ") fmt.Print(s) //Gophers!!! // 省略几个TrimRight, TrimXXXFunc, 也是类似的操作 func TrimSpace(s string) string fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) func TrimSuffix(s, suffix string) string 下面的Builder, Writer, Reader, Replacer, 都是基于String对象的操作,涉及整个string的数据结构,在Go中string是一段内存空间,通过数组表示,有长度,然后Builder就有Cap,Len,Grow等操作来操作string的数组大小, 读写通过不同的interface来分开,这些interface,还能支持其他数据类型 type _string struct { elements *byte // underlying bytes len int // number of bytes} type Builder type Reader func NewReader(s string) *Reader func (r *Reader) Len() int func (r *Reader) WriteTo(w io.Writer) (n int64, err error) type Replacer |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com