English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang 基础教程

Golang 控制语句

Golang 函数 & 方法

Golang 结构体

Golang 切片 & 数组

Golang 字符串(String)

Golang 指针

Golang 接口

Golang 并发

Golang 异常(Error)

Golang 其他杂项

Go 语言字符串分割

在Go语言中,字符串不同于Java,C ++,Python等其他语言。它是一系列宽度可变的字符,其中每个字符都使用UTF-8编码由一个或多个字节表示。在Go字符串中,可以使用以下函数将字符串拆分为一个切片。这些函数是在字符串包下定义的,因此,您必须在程序中导入字符串包才能访问这些函数:

1.Split:此函数将字符串拆分为由给定分隔符分隔的所有子字符串,并返回包含这些子字符串的切片。

Sintaxe:

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() {
    //Criar e inicializar strings
    str1 :=   "Bem-vindo, ao portal online, do w3codebox"
    str2 :=   "Meu cão se chama Dollar"
    str3 :=   "Eu gosto de jogar Ludo"
    //Exibir string
    fmt.Println("String" 1:   ",   str1)
    fmt.Println("String" 2:   ",   str2)
    fmt.Println("String" 3:   ",   str3)
    //Dividir a string fornecida
    //使用Split()函数
    res1 := strings.Split(str1, ",")
    res2 := strings.Split(str2, "")
    res3 := strings.Split(str3, "!")
    res4 := strings.Split("", "w3codebox, geeks)
    // Exibir resultado
    fmt.Println("\nResultado" 1:   ",   res1)
    fmt.Println("Resultado" 2:   ",   res2)
    fmt.Println("Resultado" 3:   ",   res3)
    fmt.Println("Resultado" 4:   ",   res4)
   }

Saída:

String 1:   Bem-vindo, ao portal online, do w3codebox
String 2:   Meu cão se chama Dollar
String 3:   Eu gosto de jogar Ludo
Result 1:  [Welcome  to the  online portal  of w3codebox]
Result 2:   [M   e   u   c   a   o   n   a   m   e   i   s   D   o   l   l   a   r]
Result 3:   [Eu gosto de jogar Ludo]
Result 4:  []

2. SplitAfter:Esta função divide a string em todos os substrings após cada instância do delimitador e retorna um slice contendo esses substrings.

Sintaxe:

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() {
    //Criar e inicializar strings
    str1 :=   "Bem-vindo, ao portal online, do w3codebox"
    str2 :=   "Meu cão se chama Dollar"
    str3 :=   "Eu gosto de jogar Ludo"
    //Exibir string
    fmt.Println("String" 1:   ",   str1)
    fmt.Println("String" 2:   ",   str2)
    fmt.Println("String" 3:   ",   str3)
    //Dividir a string fornecida
    //使用SplitAfter()函数
    res1 := strings.SplitAfter(str1, ",")
    res2 := strings.SplitAfter(str2, "")
    res3 := strings.SplitAfter(str3, "!")
    res4 := strings.SplitAfter("", "w3codebox, geeks)
    //Exibir resultado
    fmt.Println("\nResultado" 1:   ",   res1)
    fmt.Println("Resultado" 2:   ",   res2)
    fmt.Println("Resultado" 3:   ",   res3)
    fmt.Println("Resultado" 4:   ",   res4)
}

Saída:

String 1:   Bem-vindo, ao portal online, do w3codebox
String 2:   Meu cão se chama Dollar
String 3:   Eu gosto de jogar Ludo
Result 1:   [Bem-vindo,   ao   portal   online,   do   w]3codebox]
Result 2:   [M   e   u   c   a   o   n   a   m   e   i   s   D   o   l   l   a   r]
Result 3:   [Eu gosto de jogar Ludo]

3. SplitAfterN:Esta função divide a string em todos os substrings após cada instância do delimitador e retorna um slice contendo esses substrings.

Sintaxe:

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

aqui,stré uma string,sepé o delimitador, m é usado para encontrar o número de substrings a serem retornados. Aqui, sem > 0então ele retornaráentão ele retornará nil. Sem < 0então ele retornará todos os substrings.Se m > 0,Se m == 0,Se m < 0,

package main 
  
import ( 
    "fmt"
    "strings"
) 
  
func main() { 
  
    //Criar e inicializar strings
    str1 :=   "Bem-vindo, ao portal online, do w3codebox"
    str2 :=   "Meu cão se chama Dollar"
    str3 :=   "Eu gosto de jogar Ludo"
  
    //Exibir string
    fmt.Println("String" 1:   ",   str1) 
    fmt.Println("String" 2:   ",   str2) 
    fmt.Println("String" 3:   ",   str3) 
  
    //Dividir a string fornecida
    //Usar a função SplitAfterN()
    res1 :=   strings.SplitAfterN(str1,   ",", 2) 
    res2 :=   strings.SplitAfterN(str2,   "", 4) 
    res3 :=   strings.SplitAfterN(str3,   "!", 1) 
    res4 :=   strings.SplitAfterN("",   "w3codebox, geeks 3) 
  
    //Exibir resultado 
    fmt.Println("\nResultado" 1:   ",   res1) 
    fmt.Println("Resultado" 2:   ",   res2) 
    fmt.Println("Resultado" 3:   ",   res3) 
    fmt.Println("Resultado" 4:   ",   res4) 
  
}

Saída:

String 1:   Bem-vindo, ao portal online, do w3codebox
String 2:   Meu cão se chama Dollar
String 3:   Eu gosto de jogar Ludo
Result 1:   [Bem-vindo, ao portal online, do w3codebox]
Result 2:   [Meu cão se chama Dollar]
Result 3:   [Eu gosto de jogar Ludo]
Result 4:  []