doulilou8560
2017-05-20 12:51
浏览 41
已采纳

如何将字符串切片转换为符文切片

how can I convert type []string to []rune?

I know you can do it like this:
[]rune(strings.Join(array,""))
but is there a better way?

图片转代码服务由CSDN问答提供 功能建议

如何将类型 [] string 转换为 [] rune

我知道您可以这样操作:
<代码> [] rune(strings.Join(array,“”)) < br> 但有更好的方法吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanfeng3879 2017-05-20 14:28
    已采纳

    I would prefer not to use strings.Join(array,"") for this purpose because it builds one big new string I don't need. Making a big string I don't need is not space-efficient, and depending on input and hardware it may not be time-efficient.

    So instead I would iterate through the array of string values and convert each string to a rune slice, and use the built-in variadic append function to grow my slice of all rune values:

    var allRunes []rune
    for _, str := range array {
        allRunes = append(allRunes, []rune(str)...)
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题