阅读 237

Go 字符串修剪,删除前后空格的方法

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

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

语法:

func Trim(str string, cutstr string) string

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

示例

package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    //使用简写声明    str1 := "!!Welcome to nhooo !!"    str2 := "@@This is the tutorial of Golang$$"    //显示字符串    fmt.Println("修剪前的字符串:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)

    //修剪给定的字符串    // 使用 Trim() 函数    res1 := strings.Trim(str1, "!")
    res2 := strings.Trim(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 nhoooResult 2: This is the tutorial of Golang

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

语法:

func TrimLeft(str string, cutstr string) string

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

示例

//从字符串修剪左侧元素package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    //使用简写声明    str1 := "!!Welcome to nhooo **"    str2 := "@@This is the tutorial of Golang$$"    // 显示字符串    fmt.Println("修剪前的字符串:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)

    // 修剪给定的字符串    // 使用 TrimLeft() 函数    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:此函数用于修剪字符串的右侧(在函数中指定)Unicode代码点。

语法:

func TrimRight(str string, cutstr string) string

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

示例

//从字符串修剪右边的元素package mainimport (
    "fmt"    "strings")func main() {

    //创建并初始化    //使用简写声明的字符串    str1 := "!!Welcome to nhooo **"    str2 := "@@This is the tutorial of Golang$$"    // 显示字符串    fmt.Println("修剪前的字符串:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)

    //修剪给定的字符串    // 使用 TrimRight() 函数    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 nhoooResult 2: @@This is the tutorial of Golang

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

语法:

func TrimSpace(str string) string

示例

//删除字符串前后空格package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    //使用简写声明    str1 := "   **Welcome to nhooo**   "    str2 := "  ##This is the tutorial of Golang##  "    //显示字符串    fmt.Println("字符串修剪前:")
    fmt.Println(str1, str2)

    //从给定的字符串中修剪空格    //使用TrimSpace()函数    res1 := strings.TrimSpace(str1)
    res2 := strings.TrimSpace(str2)

    // 显示结果    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:此方法用于修剪给定字符串中的尾随后缀字符串。如果给定的字符串不包含指定的后缀字符串,则此函数将返回原始字符串,而不进行任何更改。

语法:

func TrimSuffix(str, suffstr string) string

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

示例

//修剪后缀字符串//给定的字符串package mainimport (
    "fmt"    "strings")func main() {

    //创建和初始化字符串    //使用简写声明    str1 := "Welcome, nhooo"    str2 := "This is the, tutorial of Golang"    //显示字符串    fmt.Println("字符串修剪前:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)

    //从给定的字符串中修剪后缀字符串    //使用TrimSuffix()函数    res1 := strings.TrimSuffix(str1, "nhooo")
    res2 := strings.TrimSuffix(str2, "Hello")

    //显示结果    fmt.Println("\n字符串修剪后:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

字符串修剪前:String 1:  Welcome, nhoooString 2: This is the, tutorial of Golang

字符串修剪后:Result 1:  Welcome,Result 2: This is the, tutorial of Golang

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

语法:

func TrimPrefix(str, suffstr string) string

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

示例

//从中修剪前缀字符串//给定的字符串package main 
  import ( 
    "fmt"    "strings") 
  func main() { 
  //创建和初始化字符串//使用简写声明    str1 := "Welcome, nhooo"    str2 := "This is the, tutorial of Golang"  
    //显示字符串    fmt.Println("字符串修剪前:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  //从给定的字符串中修剪前缀字符串//使用TrimPrefix()函数    res1 := strings.TrimPrefix(str1, "Welcome") 
    res2 := strings.TrimPrefix(str2, "Hello") 
  
    //显示结果    fmt.Println("\n字符串修剪后:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}

输出:

字符串修剪前:String 1:  Welcome, nhoooString 2: This is the, tutorial of Golang

字符串修剪后:Result 1:  , nhoooResult 2: This is the, tutorial of Golang


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐