Golang: 如何去除字符串中特定字符左边的部分
可以使用以下方式在 Golang 中去除字符串中特定字符左边的部分:
- 使用
strings.Split()函数将字符串按照特定字符分割成一个字符串数组。 - 取数组中的第二个元素,即原字符串中特定字符右边的部分。
代码示例:
import "strings"
func removeLeft(str string, sep string) string {
arr := strings.Split(str, sep)
if len(arr) > 1 {
return arr[1]
}
return str
}
使用示例:
str := 'abc::def::ghi'
sep := '::'
result := removeLeft(str, sep)
fmt.Println(result) // 'def::ghi'
原文地址: https://www.cveoy.top/t/topic/ozb5 著作权归作者所有。请勿转载和采集!