删除来自保存在 NSString 中的 html 字符串

发布于 2024-12-25 05:30:55 字数 327 浏览 3 评论 0原文

NSString* test = @"<b> Terms and Conditions</b><br />
- No  smoking(by law) <br />
 &amp;nbsp; No chewing pan(by law)<br />"
[test stringByReplacingOccurrencesOfString:@"&amp;nbsp;" withString:@"s"];

我尝试删除该 "&nbsp;" 但失败了?有什么想法吗?

谢谢

NSString* test = @"<b> Terms and Conditions</b><br />
- No  smoking(by law) <br />
 &nbsp; No chewing pan(by law)<br />"
[test stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"s"];

I tried to remove that "&nbsp;" but it fails to do so? Any ideas??

Thanks

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

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

发布评论

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

评论(4

流绪微梦 2025-01-01 05:30:55

test 是一个不可变的字符串,在进行替换时您会忽略结果。试试这个:

test = [test stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"s"];

test is an immutable string and you are ignoring the result when you do the replacement. Try this:

test = [test stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"s"];
蓝礼 2025-01-01 05:30:55

此方法不会更改您向其发送消息的字符串,而是返回一个新字符串。

NSString *newStr = [test stringByReplacingOccurrencesOfString:@"&nbsp;"
                                                   withString:@"s"];

This method doesn't change the string to which you send the message, but rather returns a new string.

NSString *newStr = [test stringByReplacingOccurrencesOfString:@"&nbsp;"
                                                   withString:@"s"];
尴尬癌患者 2025-01-01 05:30:55

我尝试使用以下单元测试来操作该字符串:

- (void) testStringManipulation {
    NSString* test = @"<b> Terms and Conditions</b><br />   - No  smoking(by law) <br /> &nbsp; No chewing pan(by law)<br />";
    NSString* expected = @"<b> Terms and Conditions</b><br />   - No  smoking(by law) <br /> s No chewing pan(by law)<br />";
    NSString *result = [test stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"s"];
    STAssertTrue([expected isEqualToString:result], @"Should be true");
}

这会在我的 IDE 中通过。
我正在使用 OCUnit。

您是否将[test stringByReplacingOccurrencesOfString... 放入新变量中?
此方法不会改变对象,但返回一个新的字符串实例。

I tried to manipulate the string with the following unit test:

- (void) testStringManipulation {
    NSString* test = @"<b> Terms and Conditions</b><br />   - No  smoking(by law) <br /> &nbsp; No chewing pan(by law)<br />";
    NSString* expected = @"<b> Terms and Conditions</b><br />   - No  smoking(by law) <br /> s No chewing pan(by law)<br />";
    NSString *result = [test stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"s"];
    STAssertTrue([expected isEqualToString:result], @"Should be true");
}

This passes in my IDE.
I'm using OCUnit.

Did you put the [test stringByReplacingOccurrencesOfString... in a new variable?
This method does not mutate the object, but returns a new string instance.

作业与我同在 2025-01-01 05:30:55

您可能只是尝试替换  用   代替,看看它是如何编译的。

You might just try replacing   with   instead and see how that compiles that way.

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