gpt4 book ai didi

go - 如何从 Golang 中的字符串周围删除引号

转载 作者:IT王子 更新时间:2023-10-29 01:06:33 35 4
gpt4 key购买 nike

我在 Golang 中有一个用引号括起来的字符串。我的目标是删除两侧的所有引号,但忽略字符串内部的所有引号。我应该怎么做呢?我的直觉告诉我像在 C# 中一样使用 RemoveAt 函数,但我在 Go 中没有看到类似的东西。

例如:

"hello""world"

应转换为:

hello""world

为了进一步说明,这是:

"""hello"""

会变成这样:

""hello""

因为外面的应该只被删除。

最佳答案

使用 slice expression :

s = s[1 : len(s)-1]

如果引号可能不存在,则使用:

if len(s) > 0 && s[0] == '"' {
s = s[1:]
}
if len(s) > 0 && s[len(s)-1] == '"' {
s = s[:len(s)-1]
}

playground example

关于go - 如何从 Golang 中的字符串周围删除引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44222554/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com