NSString编码特殊字符如!@#$%^& [英] NSString encoding special characters like !@#$%^&

查看:344
本文介绍了NSString编码特殊字符如!@#$%^&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编码我的NSString所以所有的特殊字符例如&

How can i encode my NSString so all the special character for example & becomes &amp and ' becomes &apos?

我不知道编码是否是正确的词,所以如果我错了,请纠正我。

I am not sure if encoding is the right word for it so please correct me if I am wrong.

感谢

推荐答案

你所说的是 HTML实体

What you are talking about is called HTML Entities.

存在一个声明解决此问题的类别: NSString + HTML

There exists a category claiming to solve this: NSString+HTML.

对于 URL Escaping (当我们使用它时)使用:

For URL Escaping (while we're at it) use this:

@nterface NSString (Escaping)

- (NSString *)percentEscapedString
- (NSString *)percentUnescapedString

@end

@implementation NSString (Escaping)

- (NSString *)percentEscapedString {
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)percentUnescapedString {
    return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

@end






编辑:由于Dave在我的参考解决方案中指出了弱点,以下是一些与其他解决方案相关的问题:


Edit: As Dave pointed out weaknesses in my references solution, here are some related questions with lots of other solutions:

  • Objective C HTML escape/unescape
  • HTML character decoding in Objective-C / Cocoa Touch

这篇关于NSString编码特殊字符如!@#$%^&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆