duanmou9228 2014-04-20 08:00
浏览 40
已采纳

在Go中遍历unicode字符串时跳过n个代码点

In Go, iterating over a string using

for i := 0; i < len(myString); i++{ 
    doSomething(myString[i])
}

only accesses individual bytes in the string, whereas iterating over a string via

for i, c := range myString{ 
    doSomething(c)
}

iterates over individual Unicode codepoints (calledrunes in Go), which may span multiple bytes.

My question is: how does one go about jumping ahead while iterating over a string with range Mystring? continue can jump ahead by one unicode codepoint, but it's not possible to just do i += 3 for instance if you want to jump ahead three codepoints. So what would be the most idiomatic way to advance forward by n codepoints?

I asked this question on the golang nuts mailing list, and it was answered, courtesy of some of the helpful folks on the list. Someone messaged me however suggesting I create a self-answered question on Stack Overflow for this, to save the next person with the same issue some trouble. That's what this is.

  • 写回答

2条回答 默认 最新

  • dozxos6346 2014-04-20 08:42
    关注

    I'd consider avoiding the conversion to []rune, and code this directly.

    skip := 0
    for _, c := range myString {
        if skip > 0 {
            skip--
            continue
        }
        skip = doSomething(c)
    }
    

    It looks inefficient to skip runes one by one like this, but it's the same amount of work as the conversion to []rune would be. The advantage of this code is that it avoids allocating the rune slice, which will be approximately 4 times larger than the original string (depending on the number of larger code points you have). Of course converting to []rune is a bit simpler so you may prefer that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致