NSString isEqualToString: 对于 \n
我有一个调用 Web 服务的 Cocoa 应用程序。当我解析来自 Web 服务的响应时,我得到一个 \n 作为 XML 中每个元素的 NSXMLParser 的foundCharacters 委托中的最后一个字符。
当我尝试执行以下操作时:
if (![string isEqualToString:@"\n"])
检查始终无法捕获换行符。有人知道字符串检查换行符的好方法吗?
我还发现奇怪的是,每个元素在每个值的末尾都有一个换行符,尽管我在 Web 服务的输出中没有看到任何证据。
对于本来应该非常容易做到的事情来说,这是一个相当大的问题。
I have an Cocoa app that calls a web service. When I parse the response from the web service I am getting a \n as the last character in my foundCharacters delegate for the NSXMLParser for each element in the XML.
When I try to do the following:
if (![string isEqualToString:@"\n"])
The check will always fails to catch the newline. Anyone know a good way to string check a newline?
I also find it odd that each element would have a newline at the end of each value, even though I see no evidence of that in the output from the web service.
This is quite the problem for something that should be exceptionally easy to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想这正是您所需要的?
text = [text stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
I think this is just what you need?
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
仅当
string
为一个字符长并且该字符为\n
时,该消息才会返回YES
。如果你想测试一个字符串是否以
\n
结尾,那么你需要使用hasSuffix:
:如果你想剥离
\n
从字符串末尾开始并使用剩下的内容,phunehehe 的解决方案是正确的。编辑:在 Jeremy W. Sherman 的回答中,他指出网络服务可能会为您提供以 CRLF 结尾的文本。这是一个有效的点,您不仅应该在行末尾查找
\n
,还应该检查完整的\r\n
序列。他提供的代码是实现这一目标的一种方法。无论您如何操作,您仍然需要删除整行分隔符,即使它是\r\n
序列。That message will only return
YES
ifstring
is one character long and that character is\n
.If you want to test whether a string ends with
\n
, then you need to usehasSuffix:
:If you want to strip
\n
from the end of the string and use what remains, phunehehe's solution is the correct one.Edit: In Jeremy W. Sherman's answer, he points out the web service may be giving you CRLF-terminated text. This is a valid point, and you should not only look for
\n
at the end the line, but also check for a full\r\n
sequence. The code he presents is one way to do that. However you do it, you still need to delete the full line separator, even if it's a\r\n
sequence.Web 服务可能会以
CRLF
终止所有行,即"\r\n"
。您使用的是 UNIX 系统,因此换行符'\n'
被视为行分隔符;回车符'\r'
保留为上一行的一部分。打印时与'\n'
效果相同。您是否已验证(例如使用strvis
)行尾字符是'\n'
而不是'\r'
?-[NSString hasSuffix:]
只会捕获您提供的特定换行符。要测试字符串是否以换行符或空格结尾,这里有一个假定的类别方法:您可以使用 phunehehe 的建议
text = [text stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]
删除任何行尾空格或换行符。The web service is likely terminating all lines with
CRLF
, i.e.,"\r\n"
. You're on a UNIX system, so the linefeed character'\n'
is taken as the line separator; the carriage return'\r'
is left as part of the previous line. When printed, it will have the same effect as'\n'
. Have you verified (using, say,strvis
) that the line-final character is'\n'
and not'\r'
?-[NSString hasSuffix:]
will only catch the specific newline character you provide. To test if the string ends in a newline or whitespace, here is a putative category method:You can use phunehehe's suggestion of
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
to remove any line-final whitespace or newlines.