在Go語言中,字符串不同于Java,C ++,Python等其他語言。它是一系列寬度可變的字符,其中每個(gè)字符都使用UTF-8編碼由一個(gè)或多個(gè)字節(jié)表示。您可以使用以下函數(shù)列表以不同的方式修剪字符串。所有這些函數(shù)都在字符串包下定義,因此您必須在程序中導(dǎo)入字符串包才能訪問這些函數(shù)。
1.Trim:此函數(shù)用于修剪此函數(shù)中指定的所有前導(dǎo)和后綴Unicode代碼點(diǎn)的字符串。
語法:
func Trim(str string, cutstr string) string
在這里,str表示當(dāng)前字符串,而cutstr表示要在給定字符串中修剪的元素。
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建和初始化字符串
//使用簡(jiǎn)寫聲明
str1 := "!!Welcome to nhooo !!"
str2 := "@@This is the tutorial of Golang$$"
//顯示字符串
fmt.Println("修剪前的字符串:")
fmt.Println("String 1: ", str1)
fmt.Println("String 2:", str2)
//修剪給定的字符串
// 使用 Trim() 函數(shù)
res1 := strings.Trim(str1, "!")
res2 := strings.Trim(str2, "@$")
// 顯示結(jié)果
fmt.Println("\n修剪后的字符串:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2:", res2)
}輸出:
修剪前的字符串: String 1: !!Welcome to nhooo !! String 2: @@This is the tutorial of Golang$$ 修剪后的字符串: Result 1: Welcome to nhooo Result 2: This is the tutorial of Golang
2. TrimLeft:此函數(shù)用于修剪字符串的左側(cè)(在函數(shù)中指定)Unicode代碼點(diǎn)。
語法:
func TrimLeft(str string, cutstr string) string
在這里,str表示當(dāng)前字符串,而cutstr表示要在給定字符串中修剪的左側(cè)元素。
//從字符串修剪左側(cè)元素
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建和初始化字符串
//使用簡(jiǎn)寫聲明
str1 := "!!Welcome to nhooo **"
str2 := "@@This is the tutorial of Golang$$"
// 顯示字符串
fmt.Println("修剪前的字符串:")
fmt.Println("String 1: ", str1)
fmt.Println("String 2:", str2)
// 修剪給定的字符串
// 使用 TrimLeft() 函數(shù)
res1 := strings.TrimLeft(str1, "!*")
res2 := strings.TrimLeft(str2, "@")
fmt.Println("\n修剪后的字符串:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2:", res2)
}輸出:
修剪前的字符串: String 1: !!Welcome to nhooo ** String 2: @@This is the tutorial of Golang$$ 修剪后的字符串: Result 1: Welcome to nhooo ** Result 2: This is the tutorial of Golang$$
3. TrimRight:此函數(shù)用于修剪字符串的右側(cè)(在函數(shù)中指定)Unicode代碼點(diǎn)。
語法:
func TrimRight(str string, cutstr string) string
在這里,str表示當(dāng)前字符串,而cutstr表示要在給定字符串中修剪的右側(cè)元素。
//從字符串修剪右邊的元素
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建并初始化
//使用簡(jiǎn)寫聲明的字符串
str1 := "!!Welcome to nhooo **"
str2 := "@@This is the tutorial of Golang$$"
// 顯示字符串
fmt.Println("修剪前的字符串:")
fmt.Println("String 1: ", str1)
fmt.Println("String 2:", str2)
//修剪給定的字符串
// 使用 TrimRight() 函數(shù)
res1 := strings.TrimRight(str1, "!*")
res2 := strings.TrimRight(str2, "$")
fmt.Println("\n修剪后的字符串:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2:", res2)
}輸出:
修剪前的字符串: String 1: !!Welcome to nhooo ** String 2: @@This is the tutorial of Golang$$ 修剪后的字符串: Result 1: !!Welcome to nhooo Result 2: @@This is the tutorial of Golang
4. TrimSpace:此函數(shù)用于修剪指定字符串中的所有前導(dǎo)和尾隨空白(空格)。
語法:
func TrimSpace(str string) string
//刪除字符串前后空格
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建和初始化字符串
//使用簡(jiǎn)寫聲明
str1 := " **Welcome to nhooo** "
str2 := " ##This is the tutorial of Golang## "
//顯示字符串
fmt.Println("字符串修剪前:")
fmt.Println(str1, str2)
//從給定的字符串中修剪空格
//使用TrimSpace()函數(shù)
res1 := strings.TrimSpace(str1)
res2 := strings.TrimSpace(str2)
// 顯示結(jié)果
fmt.Println("\n字符串修剪后:")
fmt.Println(res1, res2)
}輸出:
字符串修剪前: **Welcome to nhooo** ##This is the tutorial of Golang## 字符串修剪后: **Welcome to nhooo** ##This is the tutorial of Golang##
5. TrimSuffix:此方法用于修剪給定字符串中的尾隨后綴字符串。如果給定的字符串不包含指定的后綴字符串,則此函數(shù)將返回原始字符串,而不進(jìn)行任何更改。
語法:
func TrimSuffix(str, suffstr string) string
在這里,str表示原始字符串,suffstr表示后綴字符串。
//修剪后綴字符串
//給定的字符串
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建和初始化字符串
//使用簡(jiǎn)寫聲明
str1 := "Welcome, nhooo"
str2 := "This is the, tutorial of Golang"
//顯示字符串
fmt.Println("字符串修剪前:")
fmt.Println("String 1: ", str1)
fmt.Println("String 2:", str2)
//從給定的字符串中修剪后綴字符串
//使用TrimSuffix()函數(shù)
res1 := strings.TrimSuffix(str1, "nhooo")
res2 := strings.TrimSuffix(str2, "Hello")
//顯示結(jié)果
fmt.Println("\n字符串修剪后:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2:", res2)
}輸出:
字符串修剪前: String 1: Welcome, nhooo String 2: This is the, tutorial of Golang 字符串修剪后: Result 1: Welcome, Result 2: This is the, tutorial of Golang
6. TrimPrefix:此方法用于從給定字符串中修剪前導(dǎo)前綴字符串。如果給定的字符串不包含指定的前綴字符串,則此函數(shù)將返回原始字符串,而不進(jìn)行任何更改。
語法:
func TrimPrefix(str, suffstr string) string
在這里,str表示原始字符串,suffstr表示前綴字符串。
//從中修剪前綴字符串
//給定的字符串
package main
import (
"fmt"
"strings"
)
func main() {
//創(chuàng)建和初始化字符串
//使用簡(jiǎn)寫聲明
str1 := "Welcome, nhooo"
str2 := "This is the, tutorial of Golang"
//顯示字符串
fmt.Println("字符串修剪前:")
fmt.Println("String 1: ", str1)
fmt.Println("String 2:", str2)
//從給定的字符串中修剪前綴字符串
//使用TrimPrefix()函數(shù)
res1 := strings.TrimPrefix(str1, "Welcome")
res2 := strings.TrimPrefix(str2, "Hello")
//顯示結(jié)果
fmt.Println("\n字符串修剪后:")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2:", res2)
}輸出:
字符串修剪前: String 1: Welcome, nhooo String 2: This is the, tutorial of Golang 字符串修剪后: Result 1: , nhooo Result 2: This is the, tutorial of Golang