You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Groovy中获取字符的Unicode编码?

在Groovy中获取字符的Unicode编码

Groovy没有内置的getUnicode(char c)方法,但可以快速实现一个符合需求的工具方法:

def getUnicode(char c) {
    // 将字符转为Unicode码点的十六进制表示,补全为4位
    String hexCode = String.format('%04x', (int)c)
    return "\\u${hexCode}"
}

// 测试示例
assert getUnicode('÷') == '\\u00f7'
println getUnicode('÷') // 输出 \u00f7

如果想要更简洁的写法,也可以用闭包实现:

def getUnicode = { char c -> "\\u${String.format('%04x', (int)c)}" }

调用方式完全一致,传入目标字符就能返回对应的\u开头的Unicode编码字符串。

内容的提问来源于stack exchange,提问作者Saili Gaitonde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 10:02:02