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 := "abc"
testString := ""
ans := reverseString(testString)
fmt.Println(ans)
}
有疑问加站长微信联系(非本文作者)