在 Go 语言中,可以使用 strings.Trim() 函数来移除字符串首尾的空白字符。该函数的第一个参数是要处理的字符串,第二个参数是要移除的字符集合。如果第二个参数为空字符串,则会移除字符串首尾的所有空白字符。

以下是一个示例代码:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := '  hello world  '
	trimmedStr := strings.Trim(str, ' ')
	fmt.Println(trimmedStr) // 输出: 'hello world'
}

在上面的示例中,strings.Trim() 函数将字符串 ' hello world ' 中的首尾空白字符移除,得到了 'hello world'。

Go语言字符串首尾去除空白字符 - strings.Trim() 函数使用指南

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

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