Home >Backend Development >Golang >Introduction to some string operations in go language

Introduction to some string operations in go language

尚
forward
2020-06-15 16:16:003334browse

Introduction to some string operations in go language

Split the string by single/multiple characters

Cut the string according to the specified content

str := "hello world"
res0 :=strings.Split(str,"w")
fmt.Println(res0)	//[hello  orld]

The string str starts with ; Split at the end

str := "hello;world;"
res1 := strings.SplitAfter(str,";")	//[hello; world; ]
fmt.Println(res1)

Judge whether str contains a certain string

Return bool type, if not, it is false

str := "hello world"
fmt.Println(strings.Contains(str, "lo"), strings.Contains(str, "t")) //true false

Judge Whether it starts/ends with a certain string

str := "hello world"
//以某个字符串开始
i := strings.HasPrefix(str, "h")
j := strings.HasPrefix(str, "t")
fmt.Println(i, j) //true false
//以某个字符串结尾
i1 := strings.HasSuffix(str, "h")
j1 := strings.HasSuffix(str, "d") //是不是以d结尾的
fmt.Println(i1, j1)               //false true

Get the first/last occurrence position of the specified content in the string

str := "hello world"
// 获取指定内容在str中首次出现的位置,如果有则返回该元素索引, 如果没有则返回-1
fmt.Println(strings.Index(str, "l"), ",", strings.Index(str, "t")) //2 , -1
 
// 获取指定内容在str中最后一次出现的位置, 如果有则返回该元素索引, 如果没有则返回-1
fmt.Println(strings.LastIndex(str, "l"), ",", strings.LastIndex(str, "t")) //9 , -1

Replace The specified content in the string

//将str中的 hello 替换为 你好
str := "hello world"
fmt.Println(strings.Replace(str, "hello", "你好", 1)) //你好 world  
//最后一个参数表示如果str中有多个hello的话,只替换前n个

The number of occurrences of the specified content in the string

str := "hello world"
//字符串中包含某字符串的次数, 没有时返回0
fmt.Println(strings.Count(str, "l"), strings.Count(str, "t")) //3 0

The entire string is repeated n times

str := "hello world"
//	将字符串str整体重复n次
fmt.Println(strings.Repeat(str, 2))	// hello worldhello world

Convert to uppercase and lowercase

//转大写&小写
str1 := "HELLO world"
fmt.Println(strings.ToLower(str1))	//全体转小写 hello world
fmt.Println(strings.ToUpper(str1))	//全体转大写 HELLO WORLD

Use specified characters to link all elements in the slice into a string

//用指定字符将 string 类型的 slice 中所有元素链接成一个字符串
str4 := []string{"a","b","c","d"}
fmt.Println(strings.Join(str4,"-"))	//用-连接str4中的所有元素a-b-c-d

Remove the specified content in the string (including spaces)

str2 := "  hello world tt"
 
//去掉字符串尾指定的字符
fmt.Println(strings.TrimRight(str2,"t"))	//  hello world 字符串首时为TrimLeft()
 
//去掉字符串首尾的空格
fmt.Println(strings.TrimSpace(str2))	//hello world
 
//去掉字符串首尾指定的字符
fmt.Println(strings.Trim(str2,"t"))		//  hello world
fmt.Println(strings.Trim(str2,"ttt"))	//注意相同的字母即时数量比str的多也能去掉  hello world
fmt.Println(strings.Trim(str2,"  "))	//去除首尾空格hello world tt
fmt.Println(strings.Trim(str2,"b"))		//没有b时不报错返回原字符串  hello world tt

For more related knowledge, please pay attention to the go language tutorial column

The above is the detailed content of Introduction to some string operations in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete