golang 中 如何移除字符串两侧空白或预定义字符-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > >

golang 中 如何移除字符串两侧空白或预定义字符

作者:迹忆客 最近更新:2023/04/27 浏览次数:

在 go 语言中,字符串不同于 java、c 、python 等其他语言。它是一个可变宽度字符序列,其中每个字符都使用 utf-8 编码由一个或多个字节表示。 我们可以使用以下几种函数以不同方式来去除字符串两侧空白或预定义字符。 所有这些函数都定义在strings包下,所以必须在程序中导入strings包才能访问这些函数。

trim

trim 函数用于移除字符串两侧的 unicode 字符。

语法

func trim(str string, cutstr string) string

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

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 方法
func main() {
  
    // 创建并初始化字符串
    str1 := "!!welcome to jiyik.com !!"
    str2 := "@@this is the tutorial of golang$$"
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println("string 1: ", str1)
    fmt.println("string 2:", str2)
  
    // 移除给定字符串两侧指定的字符
    // 使用 trim() 函数
    res1 := strings.trim(str1, "!")
    res2 := strings.trim(str2, "@$")
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println("result 1: ", res1)
    fmt.println("result 2:", res2)
}

上面示例运行结果如下

strings before trimming:
string 1:  !!welcome to jiyik.com !!
string 2: @@this is the tutorial of golang$$
strings after trimming:
result 1:  welcome to jiyik.com 
result 2: this is the tutorial of golang

trimleft

trimleft 函数用于移除字符串的左侧(在函数中指定)unicode 字符。

语法

func trimleft(str string, cutstr string) string

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

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 函数
func main() {
  
    // 创建并初始化字符串
    str1 := "!!welcome to jiyik.com **"
    str2 := "@@this is the tutorial of golang$$"
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println("string 1: ", str1)
    fmt.println("string 2:", str2)
  
    // 使用 trimleft() 函数
    res1 := strings.trimleft(str1, "!*")
    res2 := strings.trimleft(str2, "@")
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println("result 1: ", res1)
    fmt.println("result 2:", res2)
}

上面示例运行结果如下

strings before trimming:
string 1:  !!welcome to jiyik.com **
string 2: @@this is the tutorial of golang$$
strings after trimming:
result 1:  welcome to jiyik.com **
result 2: this is the tutorial of golang$$

trimright

trimright 函数用于移除字符串的右侧(在函数中指定)unicode 字符。

语法

func trimright(str string, cutstr string) string

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

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 函数
func main() {
  
    // 创建并初始化字符串
    str1 := "!!welcome to jiyik.com **"
    str2 := "@@this is the tutorial of golang$$"
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println("string 1: ", str1)
    fmt.println("string 2:", str2)
  
    // 使用 trimright() 函数
    res1 := strings.trimright(str1, "!*")
    res2 := strings.trimright(str2, "$")
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println("result 1: ", res1)
    fmt.println("result 2:", res2)
}

上面示例运行结果如下

strings before trimming:
string 1:  !!welcome to jiyik.com **
string 2: @@this is the tutorial of golang$$
strings after trimming:
result 1:  !!welcome to jiyik.com 
result 2: @@this is the tutorial of golang

trimspace

trimspace 函数用于移除指定字符串中的两侧的空格。

语法

func trimspace(str string) string

这里,str 表示当前字符串

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 函数
func main() {
  
    // 创建并初始化字符串
    str1 := " ##welcome to jiyik.com## "
    str2 := " ##this is the tutorial of golang## "
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println(str1, str2)
  
    // 使用 trimleft() 函数
    res1 := strings.trimspace(str1)
    res2 := strings.trimspace(str2)
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println(res1, res2)
}

上面示例运行结果如下

strings before trimming:
 ##welcome to jiyik.com##   ##this is the tutorial of golang## 
strings after trimming:
##welcome to jiyik.com## ##this is the tutorial of golang##

trimsuffix

trimsuffix 方法用于从给定字符串中移除后缀字符串。 如果给定的字符串不包含指定的后缀字符串,则此函数返回原始字符串而不做任何更改。

语法

func trimsuffix(str, suffstr string) string

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

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 函数
func main() {
  
    // 创建并初始化字符串
    str1 := "welcome to jiyik.com"
    str2 := "this is the tutorial of golang"
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println("string 1: ", str1)
    fmt.println("string 2:", str2)
  
    // 使用 trimright() 函数
    res1 := strings.trimsuffix(str1, "jiyik.com")
    res2 := strings.trimsuffix(str2, "hello")
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println("result 1: ", res1)
    fmt.println("result 2:", res2)
}

上面示例运行结果如下

strings before trimming:
string 1:  welcome to jiyik.com
string 2: this is the tutorial of golang
strings after trimming:
result 1:  welcome to 
result 2: this is the tutorial of golang

trimprefix

trimprefix 方法用于从给定字符串中移除前缀字符串。 如果给定的字符串不包含指定的前缀字符串,则此函数返回原始字符串而不做任何更改。

语法

func trimprefix(str, suffstr string) string

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

示例

package main
  
import (
    "fmt"
    "strings"
)
  
// main 函数
func main() {
  
    // 创建并初始化字符串
    str1 := "welcome to jiyik.com"
    str2 := "this is the tutorial of golang"
  
    // 显示字符串
    fmt.println("strings before trimming:")
    fmt.println("string 1: ", str1)
    fmt.println("string 2:", str2)
  
    // 使用 trimright() 函数
    res1 := strings.trimprefix(str1, "welcome")
    res2 := strings.trimprefix(str2, "hello")
  
    // 显示结果
    fmt.println("\nstrings after trimming:")
    fmt.println("result 1: ", res1)
    fmt.println("result 2:", res2)
}

上面示例运行结果如下

strings before trimming:
string 1:  welcome to jiyik.com
string 2: this is the tutorial of golang
strings after trimming:
result 1:   to jiyik.com
result 2: this is the tutorial of golang

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

c# 中的 goto 语句

发布时间:2024/02/02 浏览次数:184 分类:编程语言

本教程演示了如何在 c# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 c# 中使用 goto 语法,并提供一些代码中的实际使用示例。

c# 将对象转换为 json 字符串

发布时间:2024/01/19 浏览次数:192 分类:编程语言

本文介绍如何将 c# 对象转换为 c# 中的 json 字符串的不同方法。它介绍了 javascriptserializer().serialize(),jsonconvert.serializeobject()和 jobject.fromobject()之类的方法。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图