这是一个简单的问题,但我还是想不出怎么做。
假设我有一根绳子:
x := "this string"'this‘和'string’之间的空格默认为常规unicode空格字符32/U+0020。如何将其转换为Go中不间断的unicode空格字符U+00A0?
发布于 2015-01-13 21:45:51
我认为一个基本的方法是创建一个简单的函数:
http://play.golang.org/p/YT8Cf917il
package main
import "fmt"
func ReplaceSpace(s string) string {
var result []rune
const badSpace = '\u0020'
for _, r := range s {
if r == badSpace {
result = append(result, '\u00A0')
continue
}
result = append(result, r)
}
return string(result)
}
func main() {
fmt.Println(ReplaceSpace("this string"))
}如果您需要更高级的操作,您可以使用
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"有关如何使用它的更多信息,请阅读http://blog.golang.org/normalization
https://stackoverflow.com/questions/27931884
复制相似问题