亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Golang 菜鳥教程

Golang 控制語句

Golang 函數(shù) & 方法

Golang 結構體

Golang 切片 & 數(shù)組

Golang 字符串(String)

Golang 指針

Golang 接口

Golang 并發(fā)

Golang 異常(Error)

Golang 其他雜項

Go 語言字符串分割

在Go語言中,字符串不同于Java,C ++,Python等其他語言。它是一系列寬度可變的字符,其中每個字符都使用UTF-8編碼由一個或多個字節(jié)表示。在Go字符串中,可以使用以下函數(shù)將字符串拆分為一個切片。這些函數(shù)是在字符串包下定義的,因此,您必須在程序中導入字符串包才能訪問這些函數(shù):

1.Split:此函數(shù)將字符串拆分為由給定分隔符分隔的所有子字符串,并返回包含這些子字符串的切片。

語法:

func Split(str, sep string) []string

在這里,str是字符串,sep是分隔符。 如果str不包含給定的sep且sep為非空,則它將返回長度為1的切片,其中僅包含str。 或者,如果sep為空,則它將在每個UTF-8序列之后拆分。 或者,如果str和sep均為空,則它將返回一個空切片。

package main

import (
    "fmt"
    "strings"
)

func main() {

    //創(chuàng)建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"

    //顯示字符串
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)

    //拆分給定的字符串
    //使用Split()函數(shù)
    res1 := strings.Split(str1, ",")
    res2 := strings.Split(str2, "")
    res3 := strings.Split(str3, "!")
    res4 := strings.Split("", "nhooo, geeks")

    // 顯示結果
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)
   }

輸出:

String 1:  Welcome, to the, online portal, of nhooo
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome  to the  online portal  of nhooo]
Result 2:  [M y   d o g   n a m e   i s   D o l l a r]
Result 3:  [I like to play Ludo]
Result 4:  []

2. SplitAfter:此函數(shù)在給定分隔符的每個實例之后將字符串拆分為所有子字符串,并返回包含這些子字符串的切片。

語法:

func SplitAfter(str, sep string) []string

在這里,str是字符串,sep是分隔符。 如果str不包含給定的sep且sep為非空,則它將返回長度為1的切片,其中僅包含str。 或者,如果sep為空,則它將在每個UTF-8序列之后拆分。 或者,如果str和sep均為空,則它將返回一個空切片。

package main

import (
    "fmt"
    "strings"
)

func main() {

    //創(chuàng)建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"

    //顯示字符串
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)

    //拆分給定的字符串
    //使用SplitAfter()函數(shù)
    res1 := strings.SplitAfter(str1, ",")
    res2 := strings.SplitAfter(str2, "")
    res3 := strings.SplitAfter(str3, "!")
    res4 := strings.SplitAfter("", "nhooo, geeks")

    //顯示結果
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)

}

輸出:

String 1:  Welcome, to the, online portal, of nhooo
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the,  online portal,  of nhooo]
Result 2:  [M y   d o g   n a m e   i s   D o l l a r]
Result 3:  [I like to play Ludo]

3. SplitAfterN:此函數(shù)在給定分隔符的每個實例之后將字符串拆分為所有子字符串,并返回包含這些子字符串的切片。

語法:

func SplitAfterN(str, sep string, m int) []string

在這里,str是字符串,sep是分隔符,m用于查找要返回的子字符串數(shù)。在這里,如果m> 0,那么它最多返回m個子字符串,并且最后一個字符串子字符串不會拆分。如果m == 0,則它將返回nil。如果m <0,則它將返回所有子字符串。

package main 
  
import ( 
    "fmt"
    "strings"
) 
  
func main() { 
  
    //創(chuàng)建和初始化字符串
    str1 := "Welcome, to the, online portal, of nhooo"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
  
    //顯示字符串
    fmt.Println("String 1: ", str1) 
    fmt.Println("String 2: ", str2) 
    fmt.Println("String 3: ", str3) 
  
    //拆分給定的字符串
    //使用SplitAfterN()函數(shù)
    res1 := strings.SplitAfterN(str1, ",", 2) 
    res2 := strings.SplitAfterN(str2, "", 4) 
    res3 := strings.SplitAfterN(str3, "!", 1) 
    res4 := strings.SplitAfterN("", "nhooo, geeks", 3) 
  
    //顯示結果 
    fmt.Println("\nResult 1: ", res1) 
    fmt.Println("Result 2: ", res2) 
    fmt.Println("Result 3: ", res3) 
    fmt.Println("Result 4: ", res4) 
  
}

輸出:

String 1:  Welcome, to the, online portal, of nhooo
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the, online portal, of nhooo]
Result 2:  [M y   dog name is Dollar]
Result 3:  [I like to play Ludo]
Result 4:  []