相关文章推荐
强健的蚂蚁  ·  gitlab-ci: 错误: ...·  4 月前    · 
威武的山羊  ·  Accessing mobile ...·  9 月前    · 
内向的菠萝  ·  angular - Error: ...·  1 年前    · 
买醉的海豚  ·  WPF RichTextBox ...·  1 年前    · 

Golang 菜鸟教程

  • Go 菜鸟教程
  • Go 开发环境安装
  • Go 入门教程
  • Go 语言标识符
  • Go 语言关键字
  • Go 语言数据类型
  • Go 语言变量
  • Go 语言常量
  • Go 语言运算符
  • Go 变量作用域
  • Go var关键字
  • Go 类型转换
  • Go 短变量声明(:=)
  • Golang 控制语句

  • Go 语言条件语句
  • Go 语言循环语句
  • Go 语言循环控制语句
  • Go 语言Switch语句
  • Go Select和deadlock死锁
  • Golang 函数 & 方法

  • Go 语言函数
  • Go 变参函数
  • Go 匿名函数
  • Go main和init函数
  • Go 函数参数
  • Go 函数多返回值
  • Go 返回参数命名
  • Go 空白标识符(下划线)
  • Go Defer关键字
  • Go 方法(Method)
  • Go 同名方法
  • Golang 结构体

  • Go 结构体(struct)
  • Go 结构体比较
  • Go 嵌套结构体
  • Go 匿名结构和字段
  • Go 函数用作结构体字段
  • Golang 切片 & 数组

  • Go 语言数组
  • Go 数组复制
  • Go 数组作为函数参数
  • Go 语言切片(Slice)
  • Go 切片复制
  • Go 切片作为函数参数
  • Go 语言切片比较
  • Go 切片排序
  • Go 切片分割
  • Golang 字符串(String)

  • Go 语言字符串(String)
  • Go 字符串比较
  • Go 字符串拼接
  • Go 字符串修剪
  • Go 字符串分割
  • Go 字符串包含
  • Go 字符串索引
  • Golang 指针

  • Go 语言指针
  • Go 语言双指针
  • Go 指针作为函数参数
  • Go 语言函数返回指针
  • Go 数组和指针
  • Go 结构体和指针
  • Go 语言指针比较
  • Go 语言指针容量
  • Go 语言指针长度
  • Golang 接口

  • Go 语言接口(Interfaces)
  • Go 语言多个接口
  • Go 语言接口嵌套
  • Golang 并发

  • Go 并发(Goroutines)
  • Go 语言Select语句
  • Go 语言多个Goroutine
  • Go 语言通道(Channel)
  • Go 语言单向通道
  • Golang 异常(Error)

  • Go 语言 Error(错误处理)
  • Go 语言 Recover(恢复)
  • Go 语言 Panic
  • Golang 其他杂项

  • Go Regex(正则表达式)
  • Go 语言 Time(日期时间)
  • Go File I/O(文件操作)
  • Go 语言生成随机数(rand)
  • Go 语言排序(Sort)
  • Go 语言JSON
  • Go 字符串修剪,删除前后空格的方法

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

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

    语法:

    func Trim(str string, cutstr string) string

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

    package main
    import (
        "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 nhooo
    Result 2: This is the tutorial of Golang

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

    语法:

    func TrimLeft(str string, cutstr string) string

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

    //从字符串修剪左侧元素
    package main
    import (
        "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 main
    import (
        "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 nhooo
    Result 2: @@This is the tutorial of Golang

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

    语法:

    func TrimSpace(str string) string
    //删除字符串前后空格
    package main
    import (
        "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 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)
        //从给定的字符串中修剪后缀字符串
        //使用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, nhooo
    String 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, nhooo
    String 2: This is the, tutorial of Golang
    字符串修剪后:
    Result 1:  , nhooo
    Result 2: This is the, tutorial of Golang