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

前端之家收集整理的这篇文章主要介绍了ruby – 任何人都知道字符串开头的“奇怪”字符是怎么回事?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我试图在它们的数组中检测某个字符串时,我遇到了一个奇怪的问题.谁知道这里发生了什么?
(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

原文链接:https://www.f2er.com/ruby/267639.html

猜你在找的Ruby相关文章