dongxun1244 2016-07-02 16:17
浏览 233
已采纳

Golang-ToUpper()在一个字节上?

I have a []byte, b, and I want to select a single byte, b[pos] and change it too upper case (and then lower case) The bytes type has a method called ToUpper(). How can I use this for a single byte?

Calling ToUpper on single Byte

OneOfOne gave the most efficient (when calling thousands of times), I use

val = byte(unicode.ToUpper(rune(b[pos])))

in order to find the byte and change the value

b[pos] = val

Checking if byte is Upper

Sometimes, instead of changing the case of a byte, I want to check if a byte is upper or lower case; All the upper case roman-alphabet bytes are lower than the value of the lower case bytes.

func (b Board) isUpper(x int) bool {
    return b.board[x] < []byte{0x5a}[0]
}
  • 写回答

3条回答 默认 最新

  • drouie2014 2016-07-02 18:17
    关注

    For a single byte/rune, you can use unicode.ToUpper.

    b[pos] = byte(unicode.ToUpper(rune(b[pos])))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示