On Thu, Feb 27, 2020 at 1:52 PM roger peppe <rogpe...@gmail.com> wrote:
> If you really just want to reverse rune-by-rune, it's pretty > straightforward: > > func Reverse(s string) string { > r := make([]byte, 0, len(s)) > for len(s) > 0 { > _, n := utf8.DecodeLastRuneInString(s) > i := len(s) - n > r = append(r, s[i:]...) > s = s[:i] > } > return string(r) > } > > That will also deal correctly with invalid utf8 encoding - all the bytes > of the original string will be present in the result. > Another option with slightly less bookkeeping (but slightly more magic indices): https://play.golang.org/p/sfMLimbcHSj func reverse(str string) string { if len(str) == 0 { return "" } out := make([]byte, len(str)) lastoffset := len(str) for offset, r := range str { rl := utf8.RuneLen(r) copy(out[lastoffset-rl:lastoffset], str[offset:offset+rl]) lastoffset -= rl } return string(out) } > > > On Wed, 26 Feb 2020 at 14:20, <ffm2...@web.de> wrote: > >> Maybe the implementation in Java is something you could steal to save >> time. Have a look into class StringBuilder where there is a reverse() >> method. It does the reversion differently depending on whether dealing with >> UTF16 or not. >> >> Am Samstag, 15. Februar 2020 17:37:15 UTC+1 schrieb Amarjeet Anand: >>> >>> Hi >>> >>> I was wondering why isn't there built-in string reverse function. Is it >>> left intentionally because of some reason? >>> >>> Although strings are immutable in go, there are multiple ways to achieve >>> this pretty easily. But having this function inbuilt will save our time >>> because we need it quite often. >>> >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANrC0BjVdtMuPoOiyfUrW4yTO-kfqsMFhp-bsDXLqOL1yQwwmw%40mail.gmail.com.