golang 中 如何移除字符串两侧空白或预定义字符
在 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 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 javascript 中验证 google recaptcha 第 2 版
发布时间:2024/03/23 浏览次数:193 分类:javascript
-
本文演示了如何在 javascript 中验证 google recaptcha。
在 javascript 中检查字符串是否是有效的 json 字符串
发布时间:2024/03/21 浏览次数:105 分类:javascript
-
本教程描述了如何在 javascript 中检查 json 字符串是否有效。
使用 javascript 将图像转换为 base64 字符串
发布时间:2024/03/16 浏览次数:174 分类:javascript
-
本文将讨论如何通过创建画布并将图像加载到其中,并使用文件读取器方法获取图像的相应字符串,将图像转换为其 base64 字符串表示。
c# 中的 goto 语句
发布时间:2024/02/02 浏览次数:184 分类:编程语言
-
本教程演示了如何在 c# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 c# 中使用 goto 语法,并提供一些代码中的实际使用示例。
将 json 字符串转换为 c# 对象
发布时间:2024/01/19 浏览次数:101 分类:编程语言
-
本教程演示如何使用 newtonsoft.json 包或 javascriptserializer 提供的 deserializeobject 函数将 json 字符串转换为 c#
c# 将对象转换为 json 字符串
发布时间:2024/01/19 浏览次数:192 分类:编程语言
-
本文介绍如何将 c# 对象转换为 c# 中的 json 字符串的不同方法。它介绍了 javascriptserializer().serialize(),jsonconvert.serializeobject()和 jobject.fromobject()之类的方法。