百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

charlock_holmes

> 编程语言
开源

字符编码检测,由 ICU 提供

1.1K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

字符编码检测,由 ICU 提供

CharlockHolmes

使用 ICU 的 Ruby 字符编码检测库

使用方法

首先,您需要要求它

ruby
require 'charlock_holmes'

编码检测

ruby
contents = File.read('test.xml')
detection = CharlockHolmes::EncodingDetector.detect(contents)
# => {:encoding => 'UTF-8', :confidence => 100, :type => :text}

# optionally there will be a :language key as well, but
# that's mostly only returned for legacy encodings like ISO-8859-1

注意: CharlockHolmes::EncodingDetector.detect 如果无法找到编码,则会返回 nil。 对于二进制内容, :type 将被设置为 :binary 虽然重复使用一次探测器实例更有效:

ruby
detector = CharlockHolmes::EncodingDetector.new

detection1 = detector.detect(File.read('test.xml'))
detection2 = detector.detect(File.read('test2.json'))

# and so on...

字符串猴子补丁

或者,您可以直接使用 String 类中的 detect_encoding 方法

ruby
require 'charlock_holmes/string'

contents = File.read('test.xml')

detection = contents.detect_encoding

Ruby 1.9 专用

注意: 此方法仅存在于 Ruby 1.9+ 中

如果您想使用此库来检测字符串的编码标志并设置该标志,可以使用 String 类中的 detect_encoding! 方法。

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言