Go语言的字符串操作

2018-09-29  本文已影响0人  超级皮波9

获取字符串长度

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
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

字符串包含

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
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. 字符串切割

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]

}

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]

}

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]

}

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. 按照空格切割字符串

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

package main

import (
"fmt"
"strings"
)

func main() {

    str := "182 4030 6093"

    str1 := strings.Fields(str)

    fmt.Println(str1)  //  str1 = [182  4030  6093]

}

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.字符串合并

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.重复生成字符串

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

package main

import (
    "fmt"
    "strings"
)

func main() {

    str := "A"

    str1 := strings.Repeat(str,3)

    fmt.Println("str1 = ",str1)   // str1 =  AAA

}

5.替换字符串

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--->(自定义从右去除指定字符串)

package main

import (
    "fmt"
    "strings"
)

func main() {

    str := "hello Hello everyone"

    str1 := strings.TrimLeft(str,"hello")

    fmt.Println("str1 = ",str1)   //str1 =   Hello everyone

}
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 ---> 去除以指定字符结尾的字符串

上一篇下一篇

猜你喜欢

热点阅读