如何修剪字符串的前导和尾随空白? [英] How to trim leading and trailing white spaces of a string?

查看:31
本文介绍了如何修剪字符串的前导和尾随空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种是在Go中修剪字符串变量的开头和结尾空白的有效方法?

Which is the effective way to trim the leading and trailing white spaces of string variable in Go?

推荐答案

strings.TrimSpace(s)

例如,

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "\t Hello, World\n "
    fmt.Printf("%d %q\n", len(s), s)
    t := strings.TrimSpace(s)
    fmt.Printf("%d %q\n", len(t), t)
}

输出:

16 "\t Hello, World\n "
12 "Hello, World"

这篇关于如何修剪字符串的前导和尾随空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆