📜  如何在 Golang 中修剪字符串?

📅  最后修改于: 2021-10-25 02:06:39             🧑  作者: Mango

在 Go 语言中,字符串不同于Java、C++、 Python等其他语言。它是一系列可变宽度字符,其中每个字符都由一个或多个使用 UTF-8 编码的字节表示。您可以使用以下函数列表以不同方式修剪字符串。所有这些函数都定义在字符串包下,所以你必须在你的程序中导入字符串包才能访问这些函数。

1. 修剪:该函数用于修剪字符串所有在该函数中指定的前导和尾随 Unicode 代码点。

句法:

func Trim(str string, cutstr string) string

这里, str表示当前字符串, cutstr表示给定字符串要修剪的元素。

例子:

// Go program to illustrate 
// how to trim a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks !!"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using Trim() function
    res1 := strings.Trim(str1, "!")
    res2 := strings.Trim(str2, "@$")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks !!
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  Welcome to GeeksforGeeks 
Result 2: This is the tutorial of Golang

2. TrimLeft:该函数用于修剪字符串的左侧(在函数指定)Unicode 代码点。

句法:

func TrimLeft(str string, cutstr string) string

这里, str表示当前字符串, cutstr表示要在给定字符串修剪的左侧元素。

例子:

// Go program to illustrate how to
// trim left-hand side elements
// from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks **"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using TrimLeft() function
    res1 := strings.TrimLeft(str1, "!*")
    res2 := strings.TrimLeft(str2, "@")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  Welcome to GeeksforGeeks **
Result 2: This is the tutorial of Golang$$

3. TrimRight:该函数用于修剪字符串右侧(函数指定的)Unicode 码点。

句法:

func TrimRight(str string, cutstr string) string

这里, str表示当前字符串, cutstr表示要在给定字符串修剪的右侧元素。

例子:

// Go program to illustrate how to
// trim right-hand side elements
// from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing the
    // string using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks **"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using TrimRight() function
    res1 := strings.TrimRight(str1, "!*")
    res2 := strings.TrimRight(str2, "$")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  !!Welcome to GeeksforGeeks 
Result 2: @@This is the tutorial of Golang

4. TrimSpace:该函数用于修剪指定字符串的所有前导和尾随空格。

句法:

func TrimSpace(str string) string

例子:

// Go program to illustrate how to
// trim white space from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "   **Welcome to GeeksforGeeks**   "
    str2 := "  ##This is the tutorial of Golang##  "
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println(str1, str2)
  
    // Trimming white space from the given strings
    // Using TrimSpace() function
    res1 := strings.TrimSpace(str1)
    res2 := strings.TrimSpace(str2)
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println(res1, res2)
}

输出:

Strings before trimming:
   **Welcome to GeeksforGeeks**      ##This is the tutorial of Golang##  

Strings after trimming:
**Welcome to GeeksforGeeks** ##This is the tutorial of Golang##

5. TrimSuffix:该方法是用来修整从给定的字符串尾部后缀字符串。如果给定的字符串不包含指定的后缀字符串,则此函数返回原始字符串而不做任何更改。

句法:

func TrimSuffix(str, suffstr string) string

这里, str表示原始字符串, suffstr表示后缀字符串 。

例子:

// Go program to illustrate how to
// trim a suffix string from the
// given string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "Welcome, GeeksforGeeks"
    str2 := "This is the, tutorial of Golang"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming suffix string from the given strings
    // Using TrimSuffix() function
    res1 := strings.TrimSuffix(str1, "GeeksforGeeks")
    res2 := strings.TrimSuffix(str2, "Hello")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

Strings before trimming:
String 1:  Welcome, GeeksforGeeks
String 2: This is the, tutorial of Golang

Strings after trimming:
Result 1:  Welcome, 
Result 2: This is the, tutorial of Golang

6. TrimPrefix:该方法是用来修整从给定字符串前导前缀字符串。如果给定的字符串不包含指定的前缀字符串,则此函数返回原始字符串而不做任何更改。

句法:

func TrimPrefix(str, suffstr string) string

这里, str代表原始字符串, suffstr代表前缀字符串。

例子:

// Go program to illustrate how to
// trim the prefix string from the
// given string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "Welcome, GeeksforGeeks"
    str2 := "This is the, tutorial of Golang"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
  
    // Trimming prefix string from the given strings
    // Using TrimPrefix() function
    res1 := strings.TrimPrefix(str1, "Welcome")
    res2 := strings.TrimPrefix(str2, "Hello")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
}

输出:

Strings before trimming:
String 1:  Welcome, GeeksforGeeks
String 2:  This is the, tutorial of Golang

Strings after trimming:
Result 1:  , GeeksforGeeks
Result 2:  This is the, tutorial of Golang