安装包
utf8string:
$ go get -v pre.google.com/p/go.exp/utf8stringpre.google.com/p/go.exp (download)pre.google.com/p/go.exp/utf8string$
修复程序:
package mainimport ( "fmt" "strings" "unipre" "pre.google.com/p/go.exp/utf8string")func main() { str := "tthe important rôles of utf8 textn" str = strings.TrimFunc(str, unipre.IsSpace) // The wrong way fmt.Printf("%sn", str[0:len(str)/2]) // The right way u8 := utf8string.NewString(str) FirstHalf := u8.Slice(0, u8.RuneCount()/2) fmt.Printf("%sn", FirstHalf)}
输出:
the important rthe important rô
将程序修改为仅使用Go 1.2.1标准软件包:
package mainimport ( "fmt" "strings")func main() { str := "tthe important rôles of utf8 textn" str = strings.TrimSpace(str) // The wrong way fmt.Printf("%sn", str[0:len(str)/2]) // The right way r := []rune(str) FirstHalf := string(r[:len(r)/2]) fmt.Printf("%sn", FirstHalf)}
输出:
the important rthe important rô
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)