golang中strings包函数示例

golang 专栏收录该内容
10 篇文章 0 订阅

func Compare(a, b string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Compare("123","123"))
    fmt.Println(strings.Compare("123","456"))
    fmt.Println(strings.Compare("456","123"))
}

func Contains(s, substr string) bool

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Contains("seafood", "foo"))
    fmt.Println(strings.Contains("seafood", "bar"))
    fmt.Println(strings.Contains("seafood", ""))
    fmt.Println(strings.Contains("", ""))
}

func ContainsAny(s, chars string) bool

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ContainsAny("team", "i"))
    fmt.Println(strings.ContainsAny("failure", "u & i"))
    fmt.Println(strings.ContainsAny("foo", ""))
    fmt.Println(strings.ContainsAny("", ""))
}

func ContainsRune(s string, r rune) bool

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "我爱你"
    r := rune('我')
    fmt.Println(strings.ContainsRune(s, r))
}

func Count(s, sep string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Count("cheese", "e"))
    fmt.Println(strings.Count("five", "")) // before & after each rune
}

func EqualFold(s, t string) bool

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.EqualFold("Go", "go"))
}

func Fields(s string) []string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
}

func FieldsFunc(s string, f func(rune) bool) []string

package main

import (
    "fmt"
    "strings"
    "unicode"
)

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

package main

import (
    "fmt"
    "strings"
)

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

package main

import (
    "fmt"
    "strings"
)

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, sep string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Index("chicken", "ken"))
    fmt.Println(strings.Index("chicken", "dmr"))
}

func IndexAny(s, chars string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.IndexAny("chicken", "aeiouy"))
    fmt.Println(strings.IndexAny("crwth", "aeiouy"))
}

func IndexByte(s string, c byte) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.IndexByte("a chicken", 0x63))//index 'c'
}

func IndexFunc(s string, f func(rune) bool) int

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    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

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.IndexRune("chicken", 'k'))
    fmt.Println(strings.IndexRune("chicken", 'd'))
}

func Join(a []string, sep string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := []string{"foo", "bar", "baz"}
    fmt.Println(strings.Join(s, ", "))
}

func LastIndex(s, sep string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Index("go gopher", "go"))
    fmt.Println(strings.LastIndex("go gopher", "go"))
    fmt.Println(strings.LastIndex("go gopher", "rodent"))
}

func LastIndexAny(s, chars string) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.LastIndexAny("go gopher", "go"))
    fmt.Println(strings.LastIndexAny("go gopher", "rodent"))
}

func LastIndexByte(s string, c byte) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.LastIndexByte("a chicken", 0x63))
}

func LastIndexFunc(s string, f func(rune) bool) int

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.LastIndexFunc("我爱go语言我", func(r rune) bool{
        if r == '我'{
            return true
        }
        return false
    }) )
}

func Map(mapping func(rune) rune, s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    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

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println("ba" + strings.Repeat("na", 2))
}

func Replace(s, old, new string, n int) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
    fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
}

func Split(s, sep string) []string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
}

func SplitAfter(s, sep string) []string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
}

func SplitAfterN(s, sep string, n int) []string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
}

func SplitN(s, sep string, n int) []string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
    z := strings.SplitN("a,b,c", ",", 0)
    fmt.Printf("%q (nil = %v)\n", z, z == nil)
}

func Title(s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Title("her royal highness"))
}

func ToLower(s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToLower("Gopher"))
}

func ToLowerSpecial(_case unicode.SpecialCase, s string) string

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    var sc unicode.SpecialCase
    fmt.Println(strings.ToLowerSpecial(sc, "Hello Gopher"))
}

func ToTitle(s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToTitle("loud noises"))
    fmt.Println(strings.ToTitle("хлеб"))
}

func ToTitleSpecial(_case unicode.SpecialCase, s string) string

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    var sc unicode.SpecialCase
    fmt.Println(strings.ToTitleSpecial(sc, "Hello Gopher"))
}

func ToUpper(s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToUpper("Gopher"))
}

func ToUpperSpecial(_case unicode.SpecialCase, s string) string

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    var sc unicode.SpecialCase
    fmt.Println(strings.ToUpperSpecial(sc, "Hello Gopher"))
}

func Trim(s string, cutset string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
}

func TrimFunc(s string, f func(rune) bool) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimFunc("我爱go语言我", func(r rune) bool{
        if r == '我'{
            return true
        }
        return false
    }) )
}

func TrimLeft(s string, cutset string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("[%q]", strings.TrimLeft(" a!!! Achtung! Achtung! !!! ", "a !"))
}

func TrimLeftFunc(s string, f func(rune) bool) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimLeftFunc("我爱go语言我", func(r rune) bool{
        if r == '我'{
            return true
        }
        return false
    }) )
}

func TrimPrefix(s, prefix string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    var s = "Goodbye,, world!"
    s = strings.TrimPrefix(s, "Goodbye,")
    s = strings.TrimPrefix(s, "Howdy,")
    fmt.Print("Hello" + s)
}

func TrimRight(s string, cutset string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("[%q]", strings.TrimRight(" a!!! Achtung! Achtung! !!! ", "a !"))
}

func TrimRightFunc(s string, f func(rune) bool) string

1

func TrimSpace(s string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
}

func TrimSuffix(s, suffix string) string

package main

import (
    "fmt"
    "strings"
)

func main() {
    var s = "Hello, goodbye, etc!"
    s = strings.TrimSuffix(s, "goodbye, etc!")
    s = strings.TrimSuffix(s, "planet")
    fmt.Print(s, "world!")
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

白日梦想

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值