删除&字符串目标 c 中的字符

发布于 2024-08-22 12:40:32 字数 232 浏览 2 评论 0原文

我该如何删除“&”字符串中的符号。它使我的 xml 解析器失败。

我试过了

[currentParsedCharacterData setString: [currentParsedCharacterData stringByReplacingOccurrencesOfString:@"&" withString:@"and"]];

但好像没有效果

How would I go about removing the "&" symbol from a string. It's making my xml parser fail.

I have tried

[currentParsedCharacterData setString: [currentParsedCharacterData stringByReplacingOccurrencesOfString:@"&" withString:@"and"]];

But it seems to have no effect

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

残花月 2024-08-29 12:40:32

实际上,这归结为您想要优雅地处理无效的 XML。 XML 解析器正确地告诉您此 XML 无效,因此解析失败。假设您无法控制此 XML 内容,我建议对其进行预解析以查找此类常见错误,其输出将是一个经过净化的 XML 文档,成功机会更大。

要清理文档,可能就像执行搜索和替换一样简单,仅对任何 & 进行全面替换的问题是 & 的有效用途>,例如 &©。您最终会通过创建如下内容来修改 XML: andcopy;

您可以搜索“&符号空格”,但这不会捕获最后一个字符为&符号的字符串(一个out-可能很容易处理的情况)。您真正要搜索的是 & 后面没有 ; 的出现,或者在以下 ; 之前遇到任何类型的空格的出现。 因为分号本身就很好。

如果您因为需要检测此错误和其他错误而需要更多功能,我建议您转到 NSScanner 或 RegEx 匹配以搜索此内容和其他常见内容的出现清理步骤中出现错误。 XML 文件相当大的情况也很常见,因此在将它们作为内存中字符串处理时需要小心,因为这很容易导致应用程序崩溃。 NSScanner 可以很好地将其分解为可管理的块。

Really what this boils down to is you want to gracefully handle invalid XML. The XML Parser is properly telling you that this XML is invalid, and is thusly failing to parse. Assuming you have no control over this XML content, I would suggest pre-parsing it for common errors like this, the output of which would be a sanitized XML doc that has a better chance of success.

To sanitize the doc, it may be as simple as doing search and replace, the problem with just doing a blanket replace on any & is that there are valid uses of &, for example & or ©. You would end up munging the XML by creating something like this: andcopy;

You could search for "ampersand space" but that won't catch a string that has an ampersand as the last character (an out-case that might be easily handled). What you are really searching for are occurrences of & that are not followed by a ; or those of which where any type of whitespace is encountered before the following ; because the semi-colon is fine on its own.

If you need more power because you need to detect this, and other errors, I would suggest going to NSScanner or RegEx matching to search for occurrences of this and other common errors during your sanitization step. It is also very common for XML files to be rather large things, so you need to be careful when dealing with these as in-memory strings as this can easily lead to application crashes. Breaking it up into manageable chunks is something NSScanner can do very well.

陈甜 2024-08-29 12:40:32

要快速尝试,请查看 NSString

NSString* str = @"a & b";
[str stringByReplacingOccurrencesOfString:@"&" withString:@"and"]; // better replace by &

但是,您还应该处理其他字符,即 < >

For a quick attempt look at stringByReplacingOccurrencesOfString on NSString

NSString* str = @"a & b";
[str stringByReplacingOccurrencesOfString:@"&" withString:@"and"]; // better replace by &

However you should also deal with other characters i.e. < >

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