cs.asylum
(Roger Frank)
February 18, 2016, 4:27pm
1
Learning Go. Is there a strings.LastIndex that counts runes instead of bytes?
s1 := “test- 1” // using hyphen
fmt.Println(strings.LastIndex(s1, " ")) // outputs 5
s2 := “test— 2” // using long dash instead of hyphen
fmt.Println(strings.LastIndex(s2, " ")) // outputs 7
If I count runes, the space is at 5 in both and that’s what I want.
cpalone
(Cammie Palone)
February 19, 2016, 7:51pm
2
You’ll probably have to roll your own but it should be pretty simple. Range over the string- that will get you its runes, and then just keep track of the last index you’ve found a match at as you iterate.
3 Likes
bep
(Bjørn Erik Pedersen)
February 24, 2016, 11:04am
4
If this is performance critical you can also look at this:
system
(system)
Closed
May 24, 2016, 11:04am
5
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.