gpt4 book ai didi

arrays - GOLANG 从 byte slice 中修剪前导字符,直到遇到某个特定字符

转载 作者:IT王子 更新时间:2023-10-29 01:48:54 28 4
gpt4 key购买 nike

我有一段字节,其中包含一个 json 对象,该对象包含在大括号中,带有一些可变长度的前导字符。我需要剪掉前导字符,直到遇到第一个花括号。我怎样才能做到这一点?下面是一些代码,只是为了让您了解我希望它看起来像什么,“somefunction()”是我希望有人能告诉我的功能。

var b = []byte("I want this removed {here is some json}")
a := somefunction(b, "{")
fmt.Println(string(a)) // desired output: {here is some json}

最佳答案

在字节 slice 中找到 rune '{'的第一个索引并重新 slice bbytes包提供这样的功能:

var b = []byte("I want this removed {here is some json}")
b = b[bytes.IndexRune(b, '{'):]
fmt.Println(string(b))
# {here is some json}

Go Playground

关于arrays - GOLANG 从 byte slice 中修剪前导字符,直到遇到某个特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37422338/

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