Monday, October 24, 2016

Reversing a String

str := "Hello, 世界"

for len(str) > 0 {
    r, size := utf8.DecodeLastRuneInString(str)
    fmt.Printf("%c %v\n", r, size)

    str = str[:len(str)-size]
}

Reference:

https://tip.golang.org/pkg/unicode/utf8/#DecodeLastRuneInString

No comments: