dsxmwin86342 2015-01-13 21:30
浏览 58
已采纳

在Golang中将正常空间/空白转换为不间断空间?

This is a simple question, but I still can't figure out how to do it.

Say I have this string:

x := "this string"

The whitespace between 'this' and 'string' defaults to the regular unicode whitespace character 32/U+0020. How would I convert it into the non-breaking unicode whitespace character U+00A0 in Go?

  • 写回答

2条回答 默认 最新

  • dongzhong1929 2015-01-13 21:45
    关注

    I think a basic way to do it is by creating a simple function:

    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"))
    }
    

    If you need more advanced manipulations you could create something with

    "golang.org/x/text/transform"
    "golang.org/x/text/unicode/norm"
    

    Read http://blog.golang.org/normalization for more information on how to use it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题