golang之string标准库(二)

来源:互联网 发布:qq偷菜软件下载 编辑:程序博客网 时间:2024/05/23 16:42

1:Field(s string)[]string:以1个或多个空格字符串,分割字符串,返回所有子串,如果父串是空,则返回空串

    str = "helloworld hellogolang helloprograming hellotest     hellostring hello"    newstr := strings.Fields(str)    fmt.Println("Fields :", newstr)

2:FieldFunc(s string ,f func(rune) bool)[]string:按照函数f分割字符串,返回所有子串

        func split(s rune) bool {        if s == 'n' {            return true        }        return false    }    str = "helloworld hellogolang helloprograming hellotest     hellostring hello"    newstr = strings.FieldsFunc(str, split)    fmt.Println("FiledFunc :", newstr)

3:Join(a []string,sep string)string:这个函数是将一个[]string的切片通过分隔符,分割成一个字符串

    srcstr := []string{"helloworld", "helloprograming",     "hellostrings"}    sep = "hello"    dststr := strings.Join(srcstr, sep)    fmt.Println("Join  :", dststr)

4:HasPrefix(s,prefix string)bool:判断字符串s是否包含prefix前缀

    prefix := "hello"    str = "helloworld hellogolang helloprograming hellotest     hellostring hello"    ishave := strings.HasPrefix(str, prefix)    if ishave {        fmt.Println("HasPrefix exist")    } else {        fmt.Println("HasPrefix is not exist")    }

5:HasSuffix(s,suffix string)bool:判断字符串s是否包含suffix后缀

    suffix := "hello"    str = "helloworld hellogolang helloprograming hellotest     hellostring hello"    ishave = strings.HasSuffix(str, suffix)    if ishave {        fmt.Println("HasSuffix exist")    } else {        fmt.Println("HasSuffix not exist")    }

6:Map(mapping func(rune) rune,s string)string:字符串的每一个字符通过mapping函数的处理,最后返回处理好的字符串,如果处理不正确,那么就抛弃该字符

    str = "helloworld hellogolang helloprograming hellotest     hellostring hello"    func mmap(s rune) rune {    if s == 'o' {        return 'n'    }    return 'q'}    mapString := strings.Map(mmap, str)    fmt.Println("map ", mapString)

7:Repeat(s string,count int)string:将s复制count份,返回新的字符串

    dststr = strings.Repeat("hello", 3)    fmt.Println("Repeat ", dststr)

8:ToUpper(s string)string:将字符串中的所有字符转成大写

    upperstr := strings.ToUpper("hellO")    fmt.Println("ToUpper :", upperstr)

9:ToLower(s string)string:将字符串中所有的字符转成小写

    lowerstr := strings.ToLower("HELLO")    fmt.Println("ToLower :", lowerstr)

10:ToTitle(s string)string:将字符串的所有字符转成标题体

    titlestr := strings.ToTitle("hello world hello golang")    fmt.Println("ToTitle :", titlestr)

11:Title(s string)string:将字符串转成首字符大写

    fmt.Println("Title:", strings.Title("hello world hello golang"))

11:ToUpperSpecial(c unicode.SpecialCase, s string)string:该函数把s字符串里面的每个单词转化为大写,但是调用的是unicode.SpecialCase的ToUpper方法

    var SC unicode.SpecialCase    fmt.Println("ToUpperSpecial:", strings.ToUpperSpecial(SC, "Gopher"))

12:ToLowerSpecial(c unicode.SpecialCase, s string) string:该函数把s字符串里面的每个单词转化为小写,但是调用的是unicode.SpecialCase的ToLower方法

    var SC unicode.SpecialCase    fmt.Println("ToLowerSpecial:", strings.ToLowerSpecial(SC, "GOLANG"))

13:ToTitleSpecial(c unicode.SpecialCase, s string) string:该函数把s字符串里面的每个单词转化为标题体,但是调用的是unicode.SpecialCase的ToTitle方法

    var SC unicode.SpecialCase    fmt.Println("ToTitleSpecial:", strings.ToTitleSpecial(SC, "hello world"))