美文网首页
Go语言的字符串操作

Go语言的字符串操作

作者: 超级皮波9 | 来源:发表于2018-09-29 17:06 被阅读0次

    获取字符串长度

    • 已知字符串在Go语言中的本质是一个切片,如果想获得切片的长度,可以通过len()函数获取

    • 所以可以通过len() 函数来获取字符串的长度

    • 案例

    package main
    
    import "fmt"
    
    func main() {
    
        str := "abcde"
    
        length := len(str)
    
        fmt.Println("length = ",length)  //  length =  5
    }
    
    • 注意点
    1. len()函数在获取字符串的时候 , 获取的是字节数
    2. 在Go语言中 ,中文 是按照UTF-8编码的, 所以一个中文占用3个字节
    package main
    
    import "fmt"
    
    func main() {
    
        str := "好"
    
        length := len(str)
    
        fmt.Println(length)  // length = 3
    }
    
    1. 如果像获取中文的个数,而不是字节数,那么需要讲字符转换为rune类型的切片才行
    • 案例
    package main
    
    import "fmt"
    
    func main() {
    
        str := "好好学习"
    
        length := len(str)
    
        fmt.Println("length = ",length)  // length =  12
    
        var str2 []rune = []rune(str)  // 转换为rune 类型的切片  强制转换--> 转换的数据类型(要转换的数据)
    
        length2 := len(str2)
    
        fmt.Println("length2 = ",length2)  //length =  4
    }
    

    字符串的查找

    1. 查找字符串出现的位置

    从左至右查找的函数

    • 功能:
    1. 超找指定字符在字符串中的位置
    2. 会从左至右的超找, 一旦查找到指定字符就不再查找,并返回该字符的位置, 如果没有查找到返回-1
    1. func IndexByte(s string, c byte) int
    • 注意点
      只能超找字符不能查找中文
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.jianshu.com"  
    
        index := strings.IndexByte(str,'c')  // 查找字符
    
        fmt.Println("index = ",index)  // index  = 12
        
    }
    
    

    2. func IndexRune(s string, r rune) int
    • 注意点
      可以超找字符也可以查找中文
      查找中文时 一个中文占用3 个索引位置
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.简书.com"
    
        index := strings.IndexRune(str,'简')
    
        index2 := strings.IndexRune(str,'w')
    
        fmt.Println("index = ",index)  // index  = 4
    
        fmt.Println("index = ",index2)  // index  = 0
    
    }
    

    3.func IndexAny(s, chars string) int
    • 注意点
      超找时会将指定查找的字符拆分开来查找, 查找完返回其中位置最靠前的字符的位置 ----> (支持中文和字符)
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.jianshu.com"
    
        index := strings.IndexAny(str,"wjc")
        
        fmt.Println("index = ",index)  // index  = 0
        
    }
    

    4.func Index(s, sep string) int
    • 注意点
    • 超找时会将指定查找的字符看作一个整体来查找, 查找完返回其位置** ----> (支持中文和字符)
    package main
    
    import (
    "fmt"
    "strings"
    )
    
    func main() {
    
        str := "www.简书.com"
    
        index := strings.Index(str,"简书")
    
        fmt.Println("index = ",index)  // index  = 4
    
    }
    

    5.func IndexFunc(s string, f func(rune) bool) int
    • 自定义超找
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.简书.com"
    
        index := strings.IndexFunc(str,lookup)
    
        fmt.Println("index = ",index)  // index  = 13
    
    }
    
    // 自定义超找的内容
    func lookup(ch rune)bool{
        if ch == 'm' {
            return true
        }
        return false
    }
    

    从右至左查找的函数

    2.从右至左查找跟从左至右基本一样

        func LastIndex(s, sep string) int
        func LastIndexByte(s string, c byte) int
        func LastIndexAny(s, chars string) int
        func LastIndexFunc(s string, f func(rune) bool) int
    

    字符串包含

    • 判断字符串中是否包含指定的字符, 包含就返回ture 不含饭返回 false
    1.func Contains(s, substr string) bool
    • 判断字符串中是否包含指定的子串

    • 注意点

    1. 会将指定子串看成一个整体 查找
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.jianshu.com"
    
        res := strings.Contains(str,"com")
    
        fmt.Println("res = ",res)  // res  = true
    
    }
    

    2.func ContainsRune(s string, r rune) bool
    • 判断字符串中是否包含指定的字符----->(支持中文单字)
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.jianshu.com"
    
        res := strings.ContainsRune(str,'s')
    
        fmt.Println("res = ",res)  // res  = true
    
    }
    

    3.func ContainsAny(s, chars string) bool
    • 判断字符串中是否包含指定的子串

    • 注意点

    1. 会讲指定查找的子串拆分开来查找
    2. 支持中文超找
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "www.jianshu.com"
    
        res := strings.ContainsAny(str,"xyzs")
    
        fmt.Println("res = ",res)  // res  = true
    
    }
    

    4.func HasPrefix(s, prefix string) bool
    • 功能 判断字符串是否以指定字符串开头
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "2018年报表1"
    
        res := strings.HasPrefix(str,"2018")
    
        fmt.Println("res = ",res)  // res  = true
    
    }
    

    5.func HasSuffix(s, suffix string) bool
    • 功能 判断字符串是否以指定字符串结尾
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "如歌.mp3"
    
        res := strings.HasSuffix(str,".mp3")
    
        fmt.Println("res = ",res)  // res  = true
    
    }
    

    字符串比较

    1. func Compare(a, b string) int
    • 功能
    1. 判断两个字符串是否相等
    2. 如果相等返回0
    3. 如果不相等的情况下
      第一个字符串>第二个字符串-----> 返回1
      第一个字符串<第二个字符串-----> 返回-1
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "123456"
        str1 := "123456"
        str2 := "123"
        str3 := "124"
    
        res := strings.Compare(str,str1)
    
        res2 := strings.Compare(str1,str2)
        
        res3 := strings.Compare(str2,str3)
    
        fmt.Println("res = ",res)  // res  = 0
    
        fmt.Println("res2 = ",res2)  // res2 = 1
    
        fmt.Println("res3 = ",res3)  // res3 = -1
    
    }
    

    2. func EqualFold(s, t string) bool
    • 作用 判断两个字符串是否相等,相等返回ture 不相等返回 false
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "123456"
        str1 := "123456"
        str2 := "123"
        
        res := strings.EqualFold(str,str1)
    
        res2 := strings.EqualFold(str1,str2)
    
        fmt.Println("res = ",res)  // res  = true
    
        fmt.Println("res2 = ",res2)  // res2 = false
    
    }
    

    字符串转换

    1.unc ToUpper(s string) string
    • 作用 将指定的字符串所有字母变成大写的并返回一个新的字符串
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        
        str := "abcdefg"
    
        str2 := strings.ToUpper(str)
    
        fmt.Println(str2)  // str2 = ABCDEFG
    
    }
    

    2.func ToLower(s string) string
    • 作用 将指定的字符串所有字母变成小写的并返回一个新的字符串
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "ABCDEFG"
    
        str2 := strings.ToLower(str)
    
        fmt.Println(str2)  // str2 = abcdefg
    
    }
    

    3.func ToTitle(s string) string
    • 作用 指定字符串转换为大写
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "hello world this is golang language"
    
        str2 := strings.ToTitle(str)
    
        fmt.Println(str2)  // str2 = HELLO WORLD THIS IS GOLANG LANGUAGE
    
    }
    

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

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

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

    7. func Title(s string) string
    • 作用将字符串中的单词首字母大写 需要用- 和空格 来将单词隔开
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "hello world this is golang language"
    
        str2 := strings.Title(str)
    
        fmt.Println(str2)  // str2 = Hello World This Is Golang Language
    
    }
    

    字符串的拆合

    1. 字符串切割

    • func Split(s, sep string) []string

    Split作用: 按指定字符来切割字符串, 分割后不包含指定的字符

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "2018-09-29-15-59"
    
        str1 := strings.Split(str,"-")
    
        fmt.Println(sce)  // str1 = [2018 09 29 15 59]
    
    }
    

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

    SplitN的作用 按照指定字符切割字符串 并指定切割乘 几份

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "2018-09-29-15-59"
    
        str1 := strings.SplitN(str,"-",2)
    
        fmt.Println(str1)  // str1 = [2018    09-29-15-59]
    
    }
    

    • func SplitAfter(s, sep string) []string

    SplitAfter的作用按照 指定字符串切割原有字符串, 切割后包含指定的字符

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "2018-09-29-15-59"
    
        str1 := strings.SplitAfter(str,"-")
    
        fmt.Println(str1)  //  str1 = [2018- 09- 29- 15- 59]
    
    }
    

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

    SplitAfterN作用: 按照指定字符串切割原有字符串, 切割为指定的份数, 切割完包含指定字符

    package main
    
    import (
    "fmt"
    "strings"
    )
    
    func main() {
    
        str := "2018-09-29-15-59"
    
        str1 := strings.SplitAfterN(str,"-",3)
    
        fmt.Println(str1)  //  str1 = [2018-  09-  29-15-59]
    
    }
    

    2. 按照空格切割字符串

    • func Fields(s string) []string

    Fields作用 按照空格来切割字符串 (连续多余的空格按一个空格处理)

    package main
    
    import (
    "fmt"
    "strings"
    )
    
    func main() {
    
        str := "182 4030 6093"
    
        str1 := strings.Fields(str)
    
        fmt.Println(str1)  //  str1 = [182  4030  6093]
    
    }
    

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

    FieldsFunc作用 自定义分割字符串的标准

    package main
    
    import (
    "fmt"
    "strings"
    )
    
    func main() {
    
        str := "2018/09/29"
    
        str1 := strings.FieldsFunc(str,divisional)
    
        fmt.Println(str1)  //  str1 = [2018 09 29]
    
    }
    
    func divisional(ch rune)bool{
        if ch== '/' {
            return true
        }
    
        return false
    }
    

    3.字符串合并

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

    Join 按照指定字符拼接切片中的字符串

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := []string{"www","jianshu","com"}
    
        str1 := strings.Join(str,".")
    
        fmt.Println("str1 = ",str1)  // str1 = www.jianshu.com
    
    }
    

    4.重复生成字符串

    • func Repeat(s string, count int) string

    Repeat的作用 就是将字符串复制成指定份数后组成一个新的字符串

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "A"
    
        str1 := strings.Repeat(str,3)
    
        fmt.Println("str1 = ",str1)   // str1 =  AAA
    
    }
    

    5.替换字符串

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

    Replace 的作用就是将原先字符串中 指定的字符串替换成新的字符串 , 最后的一个参数指定替换几次 如果是-1就是全部替换

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "hello hello hello hello"
    
        str1 := strings.Replace(str,"h","H",-1)
    
        fmt.Println("str1 = ",str1)   //str1 =  Hello Hello Hello Hello
    
    }
    

    字符串清理

    1.func Trim(s string, cutset string) string

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

    • 作用 去除字符串前后指定的字符串
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "hello Hello everyone hello"
    
        str1 := strings.Trim(str,"hello")
    
        fmt.Println("str1 = ",str1)   //str1 =   Hello everyone 
    
    }
    

    2.func TrimLeft(s string, cutset string) string

    2.1.func TrimLeftFunc(s string, f func(rune) bool) string -->(自定义从左去除指定字符串)

    3.func TrimRight(s string, cutset string) string

    3.1.func TrimRightFunc(s string, f func(rune) bool) string--->(自定义从右去除指定字符串)

    • TrimLeft 只去除左边指定去除的字符串
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "hello Hello everyone"
    
        str1 := strings.TrimLeft(str,"hello")
    
        fmt.Println("str1 = ",str1)   //str1 =   Hello everyone
    
    }
    
    • TrimRight 只去处右边指定去除的字符串
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := " Hello everyone hello"
        
        str1 := strings.TrimRight(str,"hello")
    
        fmt.Println("str1 = ",str1)   //str1 =   Hello everyone
        
    }
    

    7.func TrimSpace(s string) string

    • 去除字符串前后的空格
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
    
        str := "        Hello World         "
    
        str1 := strings.TrimSpace(str)
    
        fmt.Println("str1 = ",str1)   //str1 = Hello everyone
    
    }
    

    8.func TrimPrefix(s, prefix string) string ---> 去除以指定字符开头的字符串

    9.func TrimSuffix(s, suffix string) string ---> 去除以指定字符结尾的字符串

    相关文章

      网友评论

          本文标题:Go语言的字符串操作

          本文链接:https://www.haomeiwen.com/subject/dbivoftx.html