Go language---basic operation of strings

The basic operation of strings in Go language is done under the stringspackage.

Contains function

Judge whether a string contains another string, return true if it contains, and return false if it doesn't

fmt.Println(strings.Contains("HelloWorld","Hello"))

Join combination

Combination of strings

s := string{
    
    "abc","def"} //定以一个字符串切片
str := strings.Join(s,"") //Join第二个参数为字符串组合方式

Insert picture description here

s :=[]string{
    
    "abc","def"}
str := strings.Join(s,"+")

Insert picture description here

Index function

The index function is responsible for finding the subscript of the first occurrence of another string in a string and returning the subscript at the same time. Return to the specific location if found, return -1 if not found

fmt.Println(strings.Index("HelloWorld","World")) //结果为5

Repeat function

Combine the specified characters multiple times

str := strings.Repeat("Hello",3)

Split function

Split with the specified separator

str := "Hello_World_Hello"
s := strings.Split(str,"_") //按照_进行拆分

Trim function

Go to the double-ended characters

str := "    HelloWorld    "
s := strings.Trim(str," ") //去掉2头空格

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/107180688