NSString isEqualToString: 对于 \n

发布于 2024-08-07 12:03:16 字数 336 浏览 2 评论 0原文

我有一个调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光的暖冬 2024-08-14 12:03:16

我想这正是您所需要的?
text = [text stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

I think this is just what you need?
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

负佳期 2024-08-14 12:03:16

当我解析来自网络服务的响应时,我得到一个 \n 作为最后一个字符......

当我尝试执行以下操作时:

if (![string isEqualToString:@"\n"])

检查总是无法捕获换行符。

仅当 string 为一个字符长并且该字符为 \n 时,该消息才会返回 YES

如果你想测试一个字符串是否以\n结尾,那么你需要使用hasSuffix:

if (![string hasSuffix:@"\n"])

如果你想剥离 \n 从字符串末尾开始并使用剩下的内容,phunehehe 的解决方案是正确的。

编辑:在 Jeremy W. Sherman 的回答中,他指出网络服务可能会为您提供以 CRLF 结尾的文本。这是一个有效的点,您不仅应该在行末尾查找 \n,还应该检查完整的 \r\n 序列。他提供的代码是实现这一目标的一种方法。无论您如何操作,您仍然需要删除整行分隔符,即使它是 \r\n 序列。

When I parse the response from the web service I am getting a \n as the last character …

When I try to do the following:

if (![string isEqualToString:@"\n"])

The check will always fails to catch the newline.

That message will only return YES if string is one character long and that character is \n.

If you want to test whether a string ends with \n, then you need to use hasSuffix::

if (![string hasSuffix:@"\n"])

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.

落花浅忆 2024-08-14 12:03:16

Web 服务可能会以 CRLF 终止所有行,即 "\r\n"。您使用的是 UNIX 系统,因此换行符 '\n' 被视为行分隔符;回车符 '\r' 保留为上一行的一部分。打印时与'\n'效果相同。您是否已验证(例如使用 strvis)行尾字符是 '\n' 而不是 '\r'

-[NSString hasSuffix:] 只会捕获您提供的特定换行符。要测试字符串是否以换行符或空格结尾,这里有一个假定的类别方法:

- (BOOL)my_hasWhitespaceOrNewlineSuffix {
  NSRange r = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
                                    options:NSBackwardsSearch];
  BOOL foundChar = r.location != NSNotFound;
  BOOL isLineFinal = foundChar && (r.location + r.length == [self length]);
  return isLineFinal;
}

您可以使用 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:

- (BOOL)my_hasWhitespaceOrNewlineSuffix {
  NSRange r = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
                                    options:NSBackwardsSearch];
  BOOL foundChar = r.location != NSNotFound;
  BOOL isLineFinal = foundChar && (r.location + r.length == [self length]);
  return isLineFinal;
}

You can use phunehehe's suggestion of text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] to remove any line-final whitespace or newlines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文