可能是一个愚蠢的事情,但有点卡在它上面......
无法修剪"["字符串中的字符,我尝试输出的内容:
package main
import (
"fmt"
"strings"
)
func main() {
s := "this[things]I would like to remove"
t := strings.Trim(s, "[")
fmt.Printf("%s\n", t)
}
// output: this[things]I would like to remove
Run Code Online (Sandbox Code Playgroud)
还尝试了所有这些,没有成功:
s := "this [ things]I would like to remove"
t := strings.Trim(s, " [ ")
// output: this [ things]I would like to remove
s := "this [ things]I would like to remove"
t := strings.Trim(s, "[")
// output: this [ things]I would like to remove
Run Code Online (Sandbox Code Playgroud)
没有用.我在这里错过了什么?
icz*_*cza 41
你错过了阅读文档.strings.Trim():
Run Code Online (Sandbox Code Playgroud)func Trim(s string, cutset string) stringTrim返回字符串s的一个片段,其中包含cutset中包含的所有前导和尾随 Unicode代码点.
[输入中的字符不在前导位置,也不在尾随位置,它位于中间,因此strings.Trim()- 行为良好 - 不会将其删除.
尝试strings.Replace()改为:
s := "this[things]I would like to remove"
t := strings.Replace(s, "[", "", -1)
fmt.Printf("%s\n", t)
Run Code Online (Sandbox Code Playgroud)
输出(在Go Playground上试试):
thisthings]I would like to remove
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32227 次 |
| 最近记录: |