go 字符串操作相关

Go语言字符串相关操作

 

1. 判断字符串开头

package main

import (
    "fmt"
    "strings"
)

func main() {

    myString := "www.topgoer.com"

    // Option 1: (Recommended)
    if strings.HasPrefix(myString, "www") {
        fmt.Println("Hello to you too")
    } else {
        fmt.Println("Goodbye")
    }
}

 

2.go 字符串分割

# 按空格分割字符串
arr := strings.Fields(s)

# 按字符串分割字符串 abc
arr := strings.Split(s,sep)

 

3.格式化字符串 fmt.Sprintf()

 

s1 :=fmt.Sprintf("%v-%v",1,2)
fmt.Println(s1)
// 1-2

 

posted on 2021-12-20 14:41  星河赵  阅读(43)  评论(0编辑  收藏  举报

导航