How to remove redundant spaces/whitespace from a string in Golang?

后端 未结 5 2060
借酒劲吻你
借酒劲吻你 2021-02-02 12:44

I was wondering how to remove:

  • All leading/trailing whitespace or new-line characters, null characters, etc.
  • Any redundant spaces within
5条回答
  •  执笔经年
    2021-02-02 13:48

    strings.Fields() splits on any amount of white space, thus:

    strings.Join(strings.Fields(strings.TrimSpace(s)), " ")
    

提交回复
热议问题