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
相关文章
在 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()之类的方法。