1. 程式人生 > >go 字符串反轉(倒序)

go 字符串反轉(倒序)

for from art bcd pac reverse str abc pre

似乎沒什麽好辦法,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

go 字符串反轉(倒序)