strings库

strings

strings.EqualFold 判断两个utf-8编码字符串是否相同

func main()  {
	s:="jeff"
	t:="jeff"
  k:= "chary"
	fmt.Println(strings.EqualFold(s,t))  // true
  fmt.Println(strings.EqualFold(s,k))  // false
}

strings.HasPrefix 判断s字符串是否有前缀字符串p

func main()  {
	s:="jeff"
	fmt.Println(strings.HasPrefix(s,"j"))  //true
	fmt.Println(strings.HasPrefix(s,"e"))  //false
}

strings.HasSuffix 判断s字符串是否有后缀缀字符串p

func main()  {
	s:="jeff"
	fmt.Println(strings.HasSuffix(s,"f"))  //true
	fmt.Println(strings.HasSuffix(s,"j"))  //false
}

strings.Count 判断s字符串有几个不重复的sep子串

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Count(s,"f"))  // 2
	fmt.Println(strings.Count(s,"kkk"))  // 0
}

strings.Contains 判断s字符串是否包含字符串p

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Contains(s,"word"))  //true
	fmt.Println(strings.Contains(s,"kkk"))  //false
}

strings.ContainsAny 判断s字符串是否包含字符p

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.ContainsAny(s,"word"))  //true
	fmt.Println(strings.ContainsAny(s,"kkk"))  //false
}

strings.ContainsRune 判断s字符串是否包含指定ASCII码值

a-z:97-122
A-Z:65-90
0-9:48-57

func main()  {
	s:="aaa hello word jeff"
	fmt.Println(strings.ContainsRune(s,97))  //true
	fmt.Println(strings.ContainsRune(s,98))  //false
}

strings.IndexByte/IndexRune 判断指定ASCII码值在s字符串中的位置,不存在返回-1

a-z:97-122
A-Z:65-90
0-9:48-57
func main()  {
	s:="hello word jeff aaa"
	fmt.Println(strings.IndexByte(s,97))  // 16
  fmt.Println(strings.IndexByte(s,'a'))  // 16
	fmt.Println(strings.IndexByte(s,98))  // -1
}

strings.Index 判断字符串p在s字符串出现的位置,不存在返回-1

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Index(s,"word"))  // 6
	fmt.Println(strings.Index(s,"h"))  // 1
	fmt.Println(strings.Index(s,"kkk"))  // -1
}

strings.IndexAny 字符串p中的任一utf-8码值在s中第一次出现的位置,没有返回-1

func main()  {
	s:="hello word jeff aaa"
	fmt.Println(strings.IndexAny(s,"ekkk"))  // 1
	fmt.Println(strings.IndexAny(s,"kkk"))  // -1
}

strings.Title 首字母都改为大写

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Title(s))  //Hello Word Jeff
  fmt.Println(strings.ToTitle(s))  //Hello Word Jeff  标题版
}

strings.ToLower 所有转小写

func main()  {
	s:="HELLO WORD JEFF"
	fmt.Println(strings.ToLower(s))  //hello word jeff
}

strings.ToUpper 所有转大写

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.ToUpper(s))  //HELLO WORD JEFF
}

strings.Repeat 拷贝n个s字符串

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Repeat(s,2))  //hello word jeffhello word jeff
}

strings.Replace 字符串替换

返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。
func main()  {
	s:="hello word jeff"
  //前面1个"h"替换为“k”
	fmt.Println(strings.Replace(s,"h","k",1))  //kello word jeff
  
  //所有的"o"替换为"k"
  fmt.Println(strings.Replace(s,"o","k",-1))  //hellk wkrd jeff
}

strings.Trim 返回用p包裹的中间字符串

func main()  {
	s:="!!!hello word jeff !!!"
	fmt.Println(strings.Trim(s,"!"))  //hello word jeff
}

strings.TrimLeft 去掉左边包裹

func main()  {
	s:="!!!hello word jeff !!!"
	fmt.Println(strings.TrimLeft(s,"!"))  //hello word jeff !!!
}

strings.TrimRight 去掉右边包裹

func main()  {
	s:="!!!hello word jeff !!!"
	fmt.Println(strings.TrimRight(s,"!"))  //!!!hello word jeff
}

strings.TrimSpace 去掉前后空格

func main()  {
	s:="   hello word jeff  "
	fmt.Println(strings.TrimSpace(s))  //hello word jeff
}

strings.TrimPrefix 返回去除s可能的前缀prefix的字符串

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.TrimPrefix(s,"hello"))  // word jeff
	fmt.Println(strings.TrimPrefix(s,"word"))  //hello word jeff
}

strings.TrimSuffix 返回去除s可能的后缀prefix的字符串

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.TrimSuffix(s,"jeff"))  //hello word
	fmt.Println(strings.TrimPrefix(s,"word"))  //hello word jeff
}

strings.Fields 字符串按照空格转为切片

func main()  {
	s:="hello word jeff"
	fmt.Println(strings.Fields(s))  //[hello word jeff]
}

strings.Split 切割

func main()  {
   s:="hello,word,jeff"
   fmt.Println(strings.Split(s,","))  //[hello word jeff]
}

strings.SplitN 切割条件

func main()  {
   s:="hello word jeff"
   fmt.Println(strings.SplitN(s,"",3))  //[h e llo word jeff]
}

strings.Join 字符串拼接

func main()  {
	s:= []string{"hello", "word", "jeff"}
	fmt.Println(strings.Join(s,","))  //hello,word,jeff
}


var NetworkSql string
for i := range in.Network {
  NetworkSql = strings.Join([]string{NetworkSql, strconv.Itoa(in.Network[i])}, ",")
}
NetworkSql = string(NetworkSql)[1:]


func main()  {
	var k string
	s:= []int{1, 2, 3}
	for i:= range(s){
		k = strings.Join([]string{ k,strconv.Itoa(i)},",")
	}
	fmt.Println(k) // ,0,1,2
}

strings.Compare 字符串比较

func Compare(a, b string) int
如果a小于b,返回-1
如果a等于b,返回0
如果a大于b,返回1

func main() {
    fmt.Println(strings.Compare("apple", "banana")) // -1
    fmt.Println(strings.Compare("banana", "banana")) // 0
    fmt.Println(strings.Compare("banana", "apple")) // 1
}
posted @ 2023-10-06 21:25  Jeff的技术栈  阅读(14)  评论(0编辑  收藏  举报
回顶部