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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM