Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ruby 1.8.7中读取字符串文件_Ruby_String_Encoding - Fatal编程技术网

在ruby 1.8.7中读取字符串文件

在ruby 1.8.7中读取字符串文件,ruby,string,encoding,Ruby,String,Encoding,我正在用ruby 1.8.7在mac上读取.strings文件。以下是irb输出。如您所见,行是以/*开头的注释字符串。但是,当我使用命令执行start\u时,当我期望它返回true时,它返回false。我怀疑是所有的\000弄乱了字符串比较 那么,如何删除\000 f = File.open("en.lproj/Localizable.strings") #<File:en.lproj/Localizable.strings> line = f.readline "/\000*\

我正在用ruby 1.8.7在mac上读取.strings文件。以下是irb输出。如您所见,
是以
/*
开头的注释字符串。但是,当我使用命令执行
start\u时,当我期望它返回true时,它返回false。我怀疑是所有的
\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
f=File.open(“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”
放线
/*这是一个字符串注释*/
行。以?(“/*”)开头
假的
嗯,试试显而易见的:

line.start_with?("/\000*\000 ")
nul字节,
“\000”
,没有任何可视表示,因此当您
放入行时,您不会看到它们,但如果您通过
cat-v
传输脚本的输出,您可能会看到它们:

/^@*^@ ^@T^@h^@i^@s^@ ^@i^@s^@ ^@a^@ ^@s^@t^@r^@i^@n^@g^@ ^@c^@o^@m^@m^@e^@n^@t^@ ^@*^@/^@^M^@
^@
cat-v
表示零字节的方式

更新:如果要删除零字节,请使用或:


我不确定
.strings
文件的格式,所以您应该弄清楚,尤其是字符串编码。看起来可能是UTF-16,但可能不是;如果它是标准的非ASCII编码,那么您需要使用它来正确地排序编码。

是的,您完全正确。我决定重新表述我的问题,因为最后我对没有空字节的字符串感兴趣。
line.tr!("\000", '')