douxuqiao6394
2016-06-05 19:27
浏览 679
已采纳

如何在Golang中将全角数字字符转换为Ascii?

How do I convert full width characters into ascii characters in golang. The input in my program is full width numbers and I need to run some computations on them, so I assume I have to write a convert function like below, Before I start mapping bytes and such I was wondering if this is indeed available in the standard go library

fullWidth:="123"
expected := "123"
func convert(input string) string {
// body
}

expected == convert(fullWidth)

图片转代码服务由CSDN问答提供 功能建议

如何在golang中将全角字符转换为ASCII字符。 我程序中的输入是全角数字,因此我需要对它们进行一些计算,因此我假设我必须编写如下转换函数,然后再开始映射字节,因此我想知道标准中是否确实可用 进入库

  fullWidth:=“ 123” 
expected:=“ 123” 
func convert(输入字符串)字符串{
 // body 
} 
 
expected  == convert(fullWidth)
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsj2014 2016-06-05 19:45
    已采纳

    You can use the width.Transformer of the golang.org/x/text package to do the transformation but the standard library does not have this functionality. x/text is one of many official sub-repositories which have weaker compatibility requirements (see here).

    Example:

    package main
    
    import (
        "fmt"
        "golang.org/x/text/width"
    )
    
    func main() {
        s := "123"
        n := width.Narrow.String(s)
        fmt.Printf("%U: %s
    ", []rune(s), s)
        fmt.Printf("%U: %s
    ", []rune(n), n)
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题