加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 任何人都知道字符串开头的“奇怪”字符是怎么回事?

发布时间:2020-12-16 21:18:59 所属栏目:百科 来源:网络整理
导读:当我试图在它们的数组中检测某个字符串时,我遇到了一个奇怪的问题.谁知道这里发生了什么? (rdb:1) p magic_string"?Time Period"(rdb:1) p magic_string.classString(rdb:1) p magic_string == "Time Period"false(rdb:1) p "Time Period".length11(rdb:1)
当我试图在它们的数组中检测某个字符串时,我遇到了一个奇怪的问题.谁知道这里发生了什么?
(rdb:1) p magic_string
"?Time Period"
(rdb:1) p magic_string.class
String
(rdb:1) p magic_string == "Time Period"
false
(rdb:1) p "Time Period".length
11
(rdb:1) p magic_string.length
14
(rdb:1) p magic_string[0].chr
"357"
(rdb:1) p magic_string[1].chr
"273"
(rdb:1) p magic_string[2].chr
"277"
(rdb:1) p magic_string[3].chr
"T"

解决方法

您的字符串在开头包含3个字节(BOM),表示编码为UTF-8.

Q: What is a BOM?

A: A byte order mark (BOM) consists of the character code U+FEFF at
the beginning of a data stream,where it can be used as a signature
defining the byte order and encoding form,primarily of unmarked
plaintext files. Under some higher level protocols,use of a BOM may
be mandatory (or prohibited) in the Unicode data stream defined in
that protocol.

source

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读