本文介绍了为XML / HTML编码NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谢谢!
有没有办法HTML中的一个字符串(NSString)在Objective-C中编码,这是.NET中的Server.HtmlEncode的一部分? / p>
解决方案
没有一个NSString方法。你必须编写自己的字符串替换功能。执行以下替换就足够了:
- '&'=>& amp; amp;
- ''=>& quot;
- '\''=>'
- '>'=>& gt
- '<'=>& lt;
这样做应该做(没有尝试过):
[[[[myStr stringByReplacingOccurrencesOfString:@&withString:@& amp;]
stringByReplacingOccurrencesOfString:@\withString:@& quot;]
stringByReplacingOccurrencesOfString:@'withString:@&#39;]
stringByReplacingOccurrencesOfString:@> withString:@& gt]
stringByReplacingOccurrencesOfString:@< withString:@& lt;];
Is there a way to HTML encode a string (NSString) in objective-c, something along the lines of Server.HtmlEncode in .NET?
Thanks!
解决方案
There isn't an NSString method that does that. You'll have to write your own function that does string replacements. It is sufficient to do the following replacements:
- '&' => "&"
- '"' => """
- '\'' => "'"
- '>' => ">"
- '<' => "<"
Something like this should do (haven't tried):
[[[[[myStr stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
stringByReplacingOccurrencesOfString: @"\"" withString: @"""]
stringByReplacingOccurrencesOfString: @"'" withString: @"'"]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
这篇关于为XML / HTML编码NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!