要从一个字符串中移除特定的子字符串,可以使用 strings.ReplaceAll() 函数。这个函数会把所有的匹配项替换为指定的字符串。

以下是一个示例代码:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := 'Hello, World! Hello, Golang!'
	remove := 'Hello, '

	result := strings.ReplaceAll(str, remove, '')

	fmt.Println(result) // 输出: World! Golang!
}

在上面的代码中,我们使用 strings.ReplaceAll() 函数将字符串 str 中的 'Hello, ' 替换为空字符串,从而移除了所有的 'Hello, '。最后,我们输出了结果字符串 'World! Golang!'。

Go 语言字符串移除特定内容 - strings.ReplaceAll() 函数使用

原文地址: https://www.cveoy.top/t/topic/peSI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录