在 Objective-C 中解析带有百分号 (%) 的文件
Parsing file with percent signs (%) in Objective-C
我正在为 fortune 文件编写解析器。 Fortune 是 *nix 平台上的一个小应用程序,它只是随机打印出一个 "fortune"。财富文件是纯文本,每份财富在其自己的行上由百分号分隔。例如:
A little suffering is good for the soul.
-- Kirk, "The Corbomite Maneuver", stardate 1514.0
%
A man either lives life as it happens to him, meets it head-on and
licks it, or he turns his back on it and starts to wither away.
-- Dr. Boyce, "The Menagerie" ("The Cage"), star date unknown
%
我发现在解析文件时,stringWithContentsOfFile returns 一个带有 % 符号的字符串。例如:
@"A little suffering is good for the soul.\n\t\t-- Kirk, \"The Corbomite Maneuver\", stardate 1514.0\n%\nA man either lives life as it happens to him, meets it head-on and\nlicks it, or he turns his back on it and starts to wither away.\n\t\t-- Dr. Boyce, \"The Menagerie\" (\"The Cage\"), stardate unknown\n%"
但是,当我对文件内容调用 componentsSeparatedByCharactersInSet 时,所有内容都被解析为字符串,百分号除外,它是 NSTaggedPointerString。当我打印出线条时,百分号消失了。
这是因为百分号是字符串的格式说明符吗?我认为在那种情况下,最初的内容拉动会避开那些。
代码如下:
NSFileManager *fileManager;
fileManager = [NSFileManager defaultManager];
NSStringEncoding stringEncoding;
// NSString *fileContents = [NSString stringWithContentsOfFile:fileName encoding:NSASCIIStringEncoding error:nil];
NSString *fileContents = [NSString stringWithContentsOfFile:fileName usedEncoding:&stringEncoding error:nil];
NSArray *fileLines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
使用的编码最终为 UTF-8。你可以看到我也尝试过指定纯 ASCII,但它产生了相同的结果。
所以问题是,如何保留百分号?或者,我可以将它用作分隔符,然后单独解析每个后续结果。
NSTaggedPointerString 只是 NSString 的子类。你可以在任何地方使用 NSString。
但是在你的字符串中
@"A little suffering is good for the soul.\n\t\t-- Kirk, \"The Corbomite Maneuver\", stardate 1514.0\n%\nA man either lives life as it happens to him, meets it head-on and\nlicks it, or he turns his back on it and starts to wither away.\n\t\t-- Dr. Boyce, \"The Menagerie\" (\"The Cage\"), stardate unknown\n%"
符号 %
不是百分号。在 Objective-C 中,百分号被声明为 %
标记
的两倍
@"%%"
您正在调用 NSLog()
但将行字符串作为格式字符串传递。类似于:
NSLog(lineString);
因此,行字符串中的任何百分比字符都被解释为格式说明符。您应该(几乎)永远不要将来自外部来源的字符串(即未在您的代码中硬编码的字符串)作为格式字符串传递给任何函数(NSLog()
、printf()
、+[NSString stringWithFormat:]
, ETC。)。这并不安全,有时您会得到意想不到的结果。
您应该始终像这样记录一个字符串:
NSLog(@"%@", lineString);
也就是说,您需要传递一个硬编码的格式字符串,并使用外部字符串作为 data 来进行格式化。
我正在为 fortune 文件编写解析器。 Fortune 是 *nix 平台上的一个小应用程序,它只是随机打印出一个 "fortune"。财富文件是纯文本,每份财富在其自己的行上由百分号分隔。例如:
A little suffering is good for the soul.
-- Kirk, "The Corbomite Maneuver", stardate 1514.0
%
A man either lives life as it happens to him, meets it head-on and
licks it, or he turns his back on it and starts to wither away.
-- Dr. Boyce, "The Menagerie" ("The Cage"), star date unknown
%
我发现在解析文件时,stringWithContentsOfFile returns 一个带有 % 符号的字符串。例如:
@"A little suffering is good for the soul.\n\t\t-- Kirk, \"The Corbomite Maneuver\", stardate 1514.0\n%\nA man either lives life as it happens to him, meets it head-on and\nlicks it, or he turns his back on it and starts to wither away.\n\t\t-- Dr. Boyce, \"The Menagerie\" (\"The Cage\"), stardate unknown\n%"
但是,当我对文件内容调用 componentsSeparatedByCharactersInSet 时,所有内容都被解析为字符串,百分号除外,它是 NSTaggedPointerString。当我打印出线条时,百分号消失了。
这是因为百分号是字符串的格式说明符吗?我认为在那种情况下,最初的内容拉动会避开那些。
代码如下:
NSFileManager *fileManager;
fileManager = [NSFileManager defaultManager];
NSStringEncoding stringEncoding;
// NSString *fileContents = [NSString stringWithContentsOfFile:fileName encoding:NSASCIIStringEncoding error:nil];
NSString *fileContents = [NSString stringWithContentsOfFile:fileName usedEncoding:&stringEncoding error:nil];
NSArray *fileLines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
使用的编码最终为 UTF-8。你可以看到我也尝试过指定纯 ASCII,但它产生了相同的结果。
所以问题是,如何保留百分号?或者,我可以将它用作分隔符,然后单独解析每个后续结果。
NSTaggedPointerString 只是 NSString 的子类。你可以在任何地方使用 NSString。
但是在你的字符串中
@"A little suffering is good for the soul.\n\t\t-- Kirk, \"The Corbomite Maneuver\", stardate 1514.0\n%\nA man either lives life as it happens to him, meets it head-on and\nlicks it, or he turns his back on it and starts to wither away.\n\t\t-- Dr. Boyce, \"The Menagerie\" (\"The Cage\"), stardate unknown\n%"
符号 %
不是百分号。在 Objective-C 中,百分号被声明为 %
标记
@"%%"
您正在调用 NSLog()
但将行字符串作为格式字符串传递。类似于:
NSLog(lineString);
因此,行字符串中的任何百分比字符都被解释为格式说明符。您应该(几乎)永远不要将来自外部来源的字符串(即未在您的代码中硬编码的字符串)作为格式字符串传递给任何函数(NSLog()
、printf()
、+[NSString stringWithFormat:]
, ETC。)。这并不安全,有时您会得到意想不到的结果。
您应该始终像这样记录一个字符串:
NSLog(@"%@", lineString);
也就是说,您需要传递一个硬编码的格式字符串,并使用外部字符串作为 data 来进行格式化。