Skip to content

字符串的15种常用用法

获取长度

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    var name string = "老王"

    fmt.Println( len(name) )                //获取 字节长度,输出:6
    fmt.Println( utf8.RuneCountInString(name) ) //获取字符长度,输出:2
}

是否以xx开头

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "老王666"

    result := strings.HasPreFix(name, "老")

fmt.Println(result)     //输出:true
}

是否以xx结尾

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "武大郎"

    result := strings.HasSuffix(name, "郎")

    fmt.Println(result)     //输出:true
}

是否包含

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "抬老子的意大利炮来"
    result := strings.Contains(name, "老子")

    fmt.Println(result)     // 输出:true
}

变大写

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "this is xiaoxie"

    result := strings.ToUpper(name)

    fmt.Println(result)     //输出:THIS IS XIAOXIE
}
//注意:result转换为大写,name不变。

变小写

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "THIS IS DAXIE"

    result := strings.ToLower(name)

    fmt.Println(result)     //输出:this is daxie
}

去两边

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "边去处啦边"

    result1 := strings.TrimRight(name, "边")     //去除右边的边
    result2 := strings.TrimLeft(name, "边")      //去除左边的边
    result3 := strings.Trim(name, "边")      //去除两边的边

    fmt.Println(result1, result2, result3)      //输出:边去除啦 去除啦边 边去除啦
}
//注意

替换

package main

import (
    "fmt"
    "strings"
)

func main() {
    name := "tionaction"

    result1 := strings.Replace(name, "tion", "RT", 1)       //找到tion,从左到右找第一个替换
    result2 := strings.Replace(name, "tion", "RT", 2)       //找到tion,从左到右找前两个替换
    result3 := strings.Replace(name, "tion", "RT", -1)      //找到tion替换为RT, 替换所有

    fmt.Println(result1, result2, result3)
}

分割

package main

import {
    "fmt"
    "strings"
}

func main() {
    name := "抬老子的意大利炮来"
    result := strings.Split(name, "的")

    //根据 '的' 进行切割,获取一个切片(类似一个数组)
    fmt.Println(result)     //[抬老子, 意大利, 炮来]
}

拼接

package main

import {
    "bytes"
    "fmt"
    "strings"
}

func main() {
    message := "我爱" + "北京天安门"
    fmt.Println(message)        //效率低

    datalist := []string{"我爱", "北京天安门"}
    result = strings.Join(datalist, "-")        //使用分隔符
    fmt.Println(datalist)       //我爱-北京天安门

    //1.10之前使用
    var buffer bytes.Buffer
    buffer.writeString("我是")
    buffer.writeString("沙雕")
    buffer.writeString("的爸爸")
    data := buffer.String()
    fmt.Println(data)
    //1.10之后使用 效率高
    var builder strings.Builder
    builder.writeString("哈哈哈")
    builder.writeString("我只会呵呵呵")
    value := builder.String()
    fmt.Println(value)      //哈哈哈我只会呵呵呵
}

string转换为int

package main

import {
    "fmt"
    "strconv"
}

func main() {
    num := "666"
    //内部调用的就是 ParseInt
    var data, _ = strconv.Atoi(num)
    fmt.Println(data)

    //整型转字符串 (strconv.ParseInt 和 strconv.FormatInt可用于处理进制转换)
    var result, err = strconv.ParseInt(num, 10, 32)
    fmt.Println(result, err)
}

int转换为string

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var result = strconv.Itoa(888)
    fmt.Println(result)
}

字符串和 "字节集合"

package main

import (
    "fmt"
    "strconv"
    "unicode/utf8"
)

func main() {
    var name string = "小树林"

  //字符串转换为一个 "字节集合"
  byteSet := []byte(name)
  fmt.Println(byteSet)

  //字节的集合转换为字符串
  byteList := []byte{230,173,136,178,155,233,189,142}
  targetString := string(byteList)
  fmt.Println(targetString)
}

字符串和 "rune集合"

package main

import (
  "fmt"
  "strconv"
  "unicode/utf8"
)

func main() {
  var name string = "wulawula"

  //将字符串转换为unicode字符码点的集合
  temSet := []rune(name)
  fmt.Println(tempSet)
  fmt.Println(temSet[0],strconv.FormatInt(int64(tempSet[0]), 16))
  fmt.Println(temSet[1],strconv.FormatInt(int64(tempSet[1]), 16))
  fmt.Println(temSet[2],strconv.FormatInt(int64(tempSet[2]), 16))

  //"rune集合" 转换为 字符串
  runeList := []rune{27493, 27804, 40784}
  targetName := string(runeList)
  fmt.Println(targetName)
}

string 和 字符

package main

import (
  "fmt"
  "unicode/utf8"
)

func main() {
  v1 := string(65)
  fmt.Println(v1)       //A

  v2, size := utf8.DecodeRuneInString("A")
  fmt.Println(v3, size) //6
}