如何在Golang中修剪字符串?

2021年3月13日17:18:01 发表评论 955 次浏览

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

1.修剪:此函数用于修剪此函数中指定的所有前导和后缀Unicode代码点的字符串。

语法如下:

func Trim(str string, cutstr string) string

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

例子:

// 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 lsbin !!"
     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 lsbin !!
String 2: @@This is the tutorial of Golang$$

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

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

语法如下:

func TrimLeft(str string, cutstr string) string

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

例子:

// 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 lsbin **"
     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 lsbin **
String 2: @@This is the tutorial of Golang$$

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

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

语法如下:

func TrimRight(str string, cutstr string) string

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

例子:

// 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 lsbin **"
     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 lsbin **
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  !!Welcome to lsbin 
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 lsbin**   "
     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 lsbin**      ##This is the tutorial of Golang##  

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

5. TrimSuffix:此方法用于从给定字符串中修剪尾随后缀字符串。如果给定的字符串不包含指定的后缀字符串, 则此函数将返回原始字符串, 而不进行任何更改。

语法如下:

func TrimSuffix(str, suffstr string) string

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

例子:

// 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, lsbin"
     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, "lsbin" )
     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, lsbin
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代表原始字串, 后缀代表前缀字符串。

例子:

// 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, lsbin"
     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, lsbin
String 2:  This is the, tutorial of Golang

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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: