有没有办法在objective-c中对字符串(NSString)进行HTML编码,这与.NET中的Server.HtmlEncode一致?
谢谢!
没有NSString方法可以做到这一点.你必须编写自己的函数来进行字符串替换.只需进行以下替换就足够了:
'&'=>"&"
'''=>"""
'\''=>"'"
'>'=>">"
'<'=>"<"
这样的事情应该做(没试过):
[[[[[myStr stringByReplacingOccurrencesOfString: @"&" withString: @"&"] stringByReplacingOccurrencesOfString: @"\"" withString: @"""] stringByReplacingOccurrencesOfString: @"'" withString: @"'"] stringByReplacingOccurrencesOfString: @">" withString: @">"] stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
我接受了Mike的工作并将其转换为NSMutableString和NSString的类别
使用以下命令为NSMutableString创建一个类别:
- (NSMutableString *)xmlSimpleUnescape { [self replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@""" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"" withString:@"-" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; return self; } - (NSMutableString *)xmlSimpleEscape { [self replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; [self replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [self length])]; return self; }
使用以下命令为NSString创建一个类别:
- (NSString *)xmlSimpleUnescapeString { NSMutableString *unescapeStr = [NSMutableString stringWithString:self]; return [unescapeStr xmlSimpleUnescape]; } - (NSString *)xmlSimpleEscapeString { NSMutableString *escapeStr = [NSMutableString stringWithString:self]; return [escapeStr xmlSimpleEscape]; }
*Swift 2.0版本*
Objective-C版本更高效,因为它对字符串进行了可变操作.但是,这是一个简单转义的快捷方法:
extension String { typealias SimpleToFromRepalceList = [(fromSubString:String,toSubString:String)] // See http://stackoverflow.com/questions/24200888/any-way-to-replace-characters-on-swift-string // func simpleReplace( mapList:SimpleToFromRepalceList ) -> String { var string = self for (fromStr, toStr) in mapList { let separatedList = string.componentsSeparatedByString(fromStr) if separatedList.count > 1 { string = separatedList.joinWithSeparator(toStr) } } return string } func xmlSimpleUnescape() -> String { let mapList : SimpleToFromRepalceList = [ ("&", "&"), (""", "\""), ("'", "'"), ("'", "'"), ("", "'"), ("", "-"), (">", ">"), ("<", "<")] return self.simpleReplace(mapList) } func xmlSimpleEscape() -> String { let mapList : SimpleToFromRepalceList = [ ("&", "&"), ("\"", """), ("'", "'"), (">", ">"), ("<", "<")] return self.simpleReplace(mapList) } }
我本可以使用NSString桥接功能来编写与NSString版本非常相似的东西,但我决定更加开心.
我使用的是Google Toolbox for Mac(适用于iPhone).特别是,请参阅GTMNSString + HTML.h和GTMNSString + XML.h中对NSString的添加.
对于URL编码:
NSString * encodedString = [originalString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
有关详细信息,请参阅Apple的NSString文档.
对于HTML编码:
查看CFXMLCreateStringByEscapingEntities,它是Core Foundation XML库的一部分,但仍应该这样做.
同样的例程忘记了十六进制数字.这是我提出的例行程序:
- (NSString*)convertEntities:(NSString*)string { NSString *returnStr = nil; if( string ) { returnStr = [ string stringByReplacingOccurrencesOfString:@"&" withString: @"&" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@""" withString:@"\"" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@"'" withString:@"'" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@"9" withString:@"'" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@"" withString:@"'" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@"" withString:@"'" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@">" withString:@">" ]; returnStr = [ returnStr stringByReplacingOccurrencesOfString:@"<" withString:@"<" ]; returnStr = [ [ NSString alloc ] initWithString:returnStr ]; } return returnStr; }