English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
No linguagem Go, a string é diferente do Java, C ++e Python, entre outros. É uma série de caracteres de largura variável, onde cada caractere usa UTF-8O código é representado por um ou mais bytes. Você pode usar a lista de funções a seguir para ajustar strings de diferentes maneiras. Todas essas funções estão definidas na pasta de strings, portanto, você deve importar o pacote de strings no programa para acessar essas funções.
1.Trim:Esta função é usada para remover todos os pontos de código Unicode de liderança e tração da string especificada nessa função.
Sintaxe:
func Trim(str string, cutstr string) string
Aqui,strrepresenta a string atual, enquantocutstrindica os elementos que devem ser removidos da string fornecida.
package main import ( "fmt" "strings" ) func main() { //Criação e inicialização de string //Uso de declaração abreviada str1 := "!!Welcome to w3codebox !!" str2 := "@@This is the tutorial of Golang$$" //Exibir string fmt.Println("string antes da ajuste:") fmt.Println("String 1: "", str1) fmt.Println("String 2: "str2) //ajustar o string fornecido // Usar a função Trim() res1 := strings.Trim(str1! res2 := strings.Trim(str2, "@$") // Exibir resultado fmt.Println("\nstring ajustada:\ fmt.Println("Result 1: "", res1) fmt.Println("Result 2: "res2) }
Saída:
string antes da ajuste: String 1: !!Welcome to w3codebox !! String 2: @@This is the tutorial of Golang$$ string ajustada: Result 1: Welcome to w3codebox Result 2: This is the tutorial of Golang
2. TrimLeft:esta função é usada para remover Unicode code points à esquerda do string (especificados na função).
Sintaxe:
func TrimLeft(str string, cutstr string) string
Aqui,strrepresenta a string atual, enquantocutstrrepresenta o elemento à esquerda que deve ser removido do string fornecido.
//remover elementos à esquerda da string package main import ( "fmt" "strings" ) func main() { //Criação e inicialização de string //Uso de declaração abreviada str1 := "!!Welcome to w3codebox **" str2 := "@@This is the tutorial of Golang$$" // Exibir string fmt.Println("string antes da ajuste:") fmt.Println("String 1: "", str1) fmt.Println("String 2: "str2) // ajustar o string fornecido // usando a função TrimLeft() res1 := strings.TrimLeft(str1, "!*" res2 := strings.TrimLeft(str2, "@") fmt.Println("\nstring ajustada:\ fmt.Println("Result 1: "", res1) fmt.Println("Result 2: "res2) }
Saída:
string antes da ajuste: String 1: !!Welcome to w3codebox ** String 2: @@This is the tutorial of Golang$$ string ajustada: Result 1: Welcome to w3codebox ** Result 2: This is the tutorial of Golang$$
3. TrimRight:esta função é usada para remover Unicode code points à direita do string (especificados na função).
Sintaxe:
func TrimRight(str string, cutstr string) string
Aqui,strrepresenta a string atual, enquantocutstrrepresenta o elemento à direita que deve ser removido do string fornecido.
//remover elementos à direita da string package main import ( "fmt" "strings" ) func main() { //criar e inicializar //usando declaração de string abreviada str1 := "!!Welcome to w3codebox **" str2 := "@@This is the tutorial of Golang$$" // Exibir string fmt.Println("string antes da ajuste:") fmt.Println("String 1: "", str1) fmt.Println("String 2: "str2) //ajustar o string fornecido // usando a função TrimRight() res1 := strings.TrimRight(str1, "!*" res2 := strings.TrimRight(str2, "$") fmt.Println("\nstring ajustada:\ fmt.Println("Result 1: "", res1) fmt.Println("Result 2: "res2) }
Saída:
string antes da ajuste: String 1: !!Welcome to w3codebox ** String 2: @@This is the tutorial of Golang$$ string ajustada: Result 1: !!Welcome to w3codebox Result 2: @@This is the tutorial of Golang
4. TrimSpace:esta função é usada para remover todos os espaços em branco leading e trailing do string especificado.
Sintaxe:
func TrimSpace(str string) string
//remover espaços em branco à esquerda e à direita do string package main import ( "fmt" "strings" ) func main() { //Criação e inicialização de string //Uso de declaração abreviada str1 := " **Bem-vindo a w3codebox** " str2 := " ##This is the tutorial of Golang## " //Exibir string fmt.Println("Corte da string antes:") fmt.Println(str1, str2) //remover espaços em branco do string fornecido //usando a função TrimSpace() res1 := strings.TrimSpace(str1) res2 := strings.TrimSpace(str2) // Exibir resultado fmt.Println("\nCorte da string após:") fmt.Println(res1, res2) }
Saída:
Corte da string antes: **Bem-vindo a w3codebox** ##Este é o tutorial do Golang## Corte da string após: **Bem-vindo a w3codebox** ##Este é o tutorial do Golang##
5. TrimSuffix:Este método é usado para remover o sufixo final da string fornecida. Se a string fornecida não contiver o sufixo especificado, esta função retornará a string original sem nenhuma alteração.
Sintaxe:
func TrimSuffix(str, suffstr string) string
Aqui,strRepresenta a string original,suffstrRepresenta o sufixo da string.
//Remover sufixo da string //string fornecida package main import ( "fmt" "strings" ) func main() { //Criação e inicialização de string //Uso de declaração abreviada str1 := "Welcome, w3codebox" str2 := "Este é o tutorial do Golang" //Exibir string fmt.Println("Corte da string antes:") fmt.Println("String 1: "", str1) fmt.Println("String 2: "str2) //Remover sufixo da string fornecida //Usando a função TrimSuffix() res1 := strings.TrimSuffix(str1, "w3codebox") res2 := strings.TrimSuffix(str2, "Hello") //Exibir resultado fmt.Println("\nCorte da string após:") fmt.Println("Result 1: "", res1) fmt.Println("Result 2: "res2) }
Saída:
Corte da string antes: String 1: Bem-vindo, w3codebox String 2Este é o tutorial do Golang Corte da string após: Result 1: Bem-vindo, Result 2Este é o tutorial do Golang
6. TrimPrefix:Este método é usado para remover o prefixo inicial da string fornecida. Se a string fornecida não contiver o prefixo especificado, esta função retornará a string original sem nenhuma alteração.
Sintaxe:
func TrimPrefix(str, suffstr string) string
Aqui,strRepresenta a string original,suffstrRepresenta a string de prefixo.
//Remover prefixo da string //string fornecida package main import ( "fmt" "strings" ) func main() { //Criação e inicialização de string //Uso de declaração abreviada str1 := "Welcome, w3codebox" str2 := "Este é o tutorial do Golang" //Exibir string fmt.Println("Corte da string antes:") fmt.Println("String 1: "", str1) fmt.Println("String 2: "str2) //Remover prefixo da string fornecida //Usando a função TrimPrefix() res1 := strings.TrimPrefix(str1, "Welcome") res2 := strings.TrimPrefix(str2, "Hello") //Exibir resultado fmt.Println("\nCorte da string após:") fmt.Println("Result 1: "", res1) fmt.Println("Result 2: "res2) }
Saída:
Corte da string antes: String 1: Bem-vindo, w3codebox String 2Este é o tutorial do Golang Corte da string após: Result 1: , w3codebox Result 2Este é o tutorial do Golang