[英]read strings file in ruby 1.8.7
I'm trying to read a .strings file on mac with ruby 1.8.7. 我正在尝试使用ruby 1.8.7在mac上读取.strings文件。 Below is the irb output. 下面是irb输出。 As you can see, line
is a comment string that starts with /*
. 如您所见, line
是以/*
开头的注释字符串。 But when I do the start_with
command, it returns false when I expected it to return true. 但是当我执行start_with
命令时,当我希望它返回true时,它返回false。 I suspect is all the \\000
that is messing up the string compare. 我怀疑是所有的\\000
弄乱了字符串比较。
So what can I do to remove the \\000
? 那么我该怎么做才能删除\\000
?
f = File.open("en.lproj/Localizable.strings")
#<File:en.lproj/Localizable.strings>
line = f.readline
"/\000*\000 \000T\000h\000i\000s\000 \000i\000s\000 \000a\000 \000s\000t\000r\000i\000n\000g\000 \000c\000o\000m\000m\000e\000n\000t\000 \000*\000/\000\r\000\n"
puts line
/* This is a string comment */
line.start_with?("/* ")
false
Um, try the obvious: 嗯,试试明显的:
line.start_with?("/\000*\000 ")
The nul bytes, "\\000"
, don't have any visual representation so you don't see them when you puts line
but you'll probably see them if you pipe your script's output through cat -v
: nul字节"\\000"
没有任何可视化表示,因此当你puts line
时你不会看到它们,但如果你通过cat -v
管道你的脚本输出,你可能会看到它们:
/^@*^@ ^@T^@h^@i^@s^@ ^@i^@s^@ ^@a^@ ^@s^@t^@r^@i^@n^@g^@ ^@c^@o^@m^@m^@e^@n^@t^@ ^@*^@/^@^M^@
The ^@
is how cat -v
represents a zero byte. ^@
是cat -v
表示零字节的方式。
UPDATE: If you want to remove the zero bytes then use tr
or tr!
更新:如果你想删除零字节,那么使用tr
或tr!
: :
line.tr!("\000", '')
I'm not sure about the format of a .strings
file so you should figure that out and figure out the string encoding in particular. 我不确定.strings
文件的格式,所以你应该弄清楚,特别是找出字符串编码。 It looks like it might be UTF-16 but maybe not; 看起来它可能是UTF-16但也许不是; if it is a standard non-ASCII encoding then you'll want to use iconv
to properly sort out the encoding. 如果它是标准的非ASCII编码,那么你将需要使用iconv
来正确地整理编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.