多字节字符和 tr 命令
Multi-byte characters and tr command
我正在尝试使用 tr
.
将一些单字节字符转换为多字节字符
当我只有一个字符要转换时,转换有效:
"\".tr('\', '\')
# => "\"
但是如果我再添加一些字符,则检测不到这些字符:
"\".tr("\¥'", "\¥’")
# => "\"
为什么会这样,如何转换多字节字符?
我正在使用 Ruby 2.3.3。此外,我得到:
"\".encoding #=> #<Encoding:UTF-8>
"\¥'".encoding #=> #<Encoding:UTF-8>
"\¥’".encoding #=> #<Encoding:UTF-8>
这是 Ruby 的错误吗?
文字反斜杠必须放在最后。来自 documentation:(强调)
The backslash character \
can be used to escape ^
or -
and is otherwise ignored unless it appears at the end of a range or the end of the from_str
or to_str
:
"\abc".tr("\abc", "/def") #=> "\/de"
相当于:
"\abc".tr("abc", "/def") #=> "\/de"
对战:
"\abc".tr("abc\", "def/") #=> "/def"
我正在尝试使用 tr
.
当我只有一个字符要转换时,转换有效:
"\".tr('\', '\')
# => "\"
但是如果我再添加一些字符,则检测不到这些字符:
"\".tr("\¥'", "\¥’")
# => "\"
为什么会这样,如何转换多字节字符?
我正在使用 Ruby 2.3.3。此外,我得到:
"\".encoding #=> #<Encoding:UTF-8>
"\¥'".encoding #=> #<Encoding:UTF-8>
"\¥’".encoding #=> #<Encoding:UTF-8>
这是 Ruby 的错误吗?
文字反斜杠必须放在最后。来自 documentation:(强调)
The backslash character
\
can be used to escape^
or-
and is otherwise ignored unless it appears at the end of a range or the end of thefrom_str
orto_str
:
"\abc".tr("\abc", "/def") #=> "\/de"
相当于:
"\abc".tr("abc", "/def") #=> "\/de"
对战:
"\abc".tr("abc\", "def/") #=> "/def"