go 字符串反转(倒序)

似乎没什么好办法,string的话也得需要先转换成rune再反转再转成string

package main

import (
"fmt"
)

func reverseString(s string) string {
runes := []rune(s)

for from, to := 0, len(runes)-1; from < to; from, to = from + 1, to - 1 {
runes[from], runes[to] = runes[to], runes[from] 
}

return string(runes)
}

func main(){
testString := "abcdefg12345"
// testString := ""


ans := reverseString(testString)

fmt.Println(ans)
}

  转自:http://blog.csdn.net/qq_15437667/article/details/51714765

posted @ 2017-07-08 20:25  大漠垂杨  阅读(5955)  评论(0编辑  收藏  举报