go Trim()方法
Trim()方法语法
func Trim(s string, cutset string) string
go源码对Trim()方法的介绍:
Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
即,golang标准库strings模块中的Trim()方法,可以移除第一个参数s字符串首尾两端的第二个参数cutset指定的子串。
参数
参数 | 描述 |
---|---|
s | 第一个参数,go string字符串类型 |
cutset | 指定Trim()方法要移除的子串 |
strings.Trim()方法实例代码
下方实例在字符串s中首尾和中间位置都设置了空格,Trim()的第二个参数指定的是空字符:
func main() {
str := strings.Trim(" 笨鸟工具, x1y1z1.com ", " ")
fmt.Println(str)
}
运行go文件,得到输出:
笨鸟工具, x1y1z1.com