golang常用函数

func len

// func len(v Type) int
// 返回 v 的长度,取决于具体类型:
// 数组:v中元素的数量
// 数组指针:*v中元素的数量(v为nil时panic)
// 切片、映射:v中元素的数量;若v为nil,len(v)即为零
// 字符串:v中字节的数量
// 通道:通道缓存中队列(未读取)元素的数量;若v为 nil,len(v)即为零
fmt.Println("len = ", len("abc")) // len =  3

[]rune

// string转rune
str1 := "abc北京"
fmt.Printf("str1字节长度:%v \n", len(str1)) //str1字节长度:9   一个汉字占3个字节
ru := []rune(str1)
fmt.Printf("str1字符长度:%v \n", len(ru)) //str1字符长度:5

[]byte

// string转byte
str1 = "abc北京"
by := []byte(str1)
fmt.Printf("by值:%v \n", by) // by值:[97 98 99 229 140 151 228 186 172]

string

// byte转string
by = []byte{97, 100, 104, 120}
str1 = string(by)
fmt.Println("str1 = ", str1) // str1 =  adhx

  

func FormatInt

// func FormatInt(i int64, base int) string
// 返回i的base进制的字符串,base 必须在2到36之间
var i int64 = 123
str := strconv.FormatInt(i, 2)
fmt.Printf("i的二进制:%s \n", str) //i的二进制:1111011

func Itoa

// int转string
// func Itoa(i int) string
//Itoa是FormatInt(i, 10) 的简写。strconv.FormatInt(i, 10)
a := 123
str = strconv.Itoa(a)
fmt.Printf("a的类型:%T,值:%s \n", str, str) //a的类型:string,值:123

func Atoi

// string转int
// func Atoi(s string) (i int, err error)
str = "456a"
b, _ := strconv.Atoi(str)
fmt.Printf("b的类型:%T,值:%d \n", b, b) //b的类型:int,值:456

 

func Contains

// func Contains(s, substr string) bool
// 判断字符串s是否包含子串substr。
fmt.Println(strings.Contains("hello", "el")) //true
fmt.Println(strings.Contains("hello", "ww")) //false
fmt.Println(strings.Contains("hello", ""))   //true

func Count

// func Count(s, sep string) int
// 返回字符串s中有几个不重复的sep子串。
fmt.Println(strings.Count("hello", "l")) //2

func EqualFold

// func EqualFold(s, t string) bool
// 判断两个字符串是否相同,不区分大小写
fmt.Println(strings.EqualFold("abc", "AbC")) //true
fmt.Println("abc" == "AbC")                  //false  区分大小写

func Index

// func Index(s, sep string) int
// 子串sep在字符串s中第一次出现的位置,不存在则返回-1。
fmt.Println(strings.Index("hello", "lo")) //3
fmt.Println(strings.Index("hello", "oo")) //-1

func LastIndex

// func LastIndex(s, sep string) int
// 子串sep在字符串s中最后一次出现的位置,不存在则返回-1。
fmt.Println(strings.LastIndex("hello", "l"))  //3
fmt.Println(strings.LastIndex("hello", "kl")) //-1

func Replace

// func Replace(s, old, new string, n int) string
//字符串替换
// 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串
// 将 "hello hello hello" 中的 "el" 替换为 "AA"
fmt.Println(strings.Replace("hello hello hello", "el", "AA", -1)) // hAAlo hAAlo hAAlo
fmt.Println(strings.Replace("hello hello hello", "el", "AA", 0))  // hello hello hello
fmt.Println(strings.Replace("hello hello hello", "el", "AA", 2))  // hAAlo hAAlo hello

func Split

// func Split(s, sep string) []string
// 将s以sep分割 返回所有片段组成的切片
fmt.Printf("%q \n", strings.Split("a,b,c", ",")) // ["a" "b" "c"]

func ToLower

// func ToLower(s string) string
// 将所有字母转化为小写 此为拷贝
str = "aBCdE"
str1 = strings.ToLower(str)
fmt.Println("str = ", str)   // str =  aBCdE
fmt.Println("str1 = ", str1) // str1 =  abcde

func ToUpper

// func ToUpper(s string) string
// 将所有字母转化为大写 此为拷贝
str = "aBCdE"
str1 = strings.ToUpper(str)
fmt.Println("str = ", str)   // str =  aBCdE
fmt.Println("str1 = ", str1) // str1 =  ABCDE

func TrimSpace

str := " hello "
fmt.Printf("%q \n", str) //" hello "
// 去掉字符串两端的空白字符
// 空白字符:'\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP).
str1 := strings.TrimSpace(str)
fmt.Printf("%q \n", str1) //"hello"

 

//func Trim(s string, cutset string) string
// 去掉字符串两端包含的字符串
str := "!hello!!"
t := strings.Trim(str, "!")
fmt.Println(t) //hello

// 去掉字符串左侧包含的字符串
// func TrimLeft(s string, cutset string) string
left := strings.TrimLeft(str, "!") // hello!!
fmt.Println(left)

// 去掉字符串右侧包含的字符串
// func TrimRight(s string, cutset string) string
right := strings.TrimRight(str, "!") //!hello
fmt.Println(right)

  

func HasPrefix

// func HasPrefix(s, prefix string) bool
// 判断s是否有前缀字符串prefix。
str := "s_hello"
has := strings.HasPrefix(str, "s_")
fmt.Println(has) //true

 

func HasSuffix

// func HasSuffix(s, suffix string) bool
// 判断s是否有后缀字符串suffix。
str := "hello.jpg"
has := strings.HasSuffix(str, "jpg") //true
fmt.Println(has)

  

 

 

  

  

  

  

  

  

  

  

  

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

posted @ 2020-08-25 11:22  懶得取名  阅读(244)  评论(0编辑  收藏  举报