Maison  >  Article  >  développement back-end  >  Introduction à quelques opérations sur les chaînes en langage Go

Introduction à quelques opérations sur les chaînes en langage Go

尚
avant
2020-06-15 16:16:003237parcourir

Introduction à quelques opérations sur les chaînes en langage Go

Diviser la chaîne par caractères simples/multiples

Couper la chaîne par contenu spécifié

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

String str to ; end

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

Détermine si str contient une certaine chaîne

Renvoie le type booléen, sinon, il est faux

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

Détermine s'il commence/se termine par une certaine chaîne

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

Obtient la première/dernière position d'occurrence du contenu spécifié dans la chaîne

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

Remplacer le spécifié contenu dans la chaîne

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

Le nombre d'occurrences du contenu spécifié dans la chaîne

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

La chaîne entière est répétée n fois

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

Convertir la casse

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

Utilisez les caractères spécifiés pour lier tous les éléments de la tranche en une chaîne

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

Supprimer le contenu spécifié (y compris les espaces) dans la chaîne

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

Pour plus de connaissances connexes, veuillez faire attention à la colonne tutoriel de langage go

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer