简体   繁体   中英

read strings file in ruby 1.8.7

I'm trying to read a .strings file on mac with ruby 1.8.7. Below is the irb output. As you can see, line is a comment string that starts with /* . But when I do the start_with command, it returns false when I expected it to return true. I suspect is all the \\000 that is messing up the string compare.

So what can I do to remove the \\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 :

/^@*^@ ^@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.

UPDATE: If you want to remove the zero bytes then use tr or 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. It looks like it might be UTF-16 but maybe not; if it is a standard non-ASCII encoding then you'll want to use iconv to properly sort out the encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM