转换& 在Objective-C中

nbo*_*jja 23 iphone escaping objective-c html-entities

我有一个以下格式的URL字符串.

http://myserver.com/_layouts/feed.aspx?xsl=4&web=%2F&page=dda3fd10-c776-4d69-8c55-2f1c74b343e2&wp=476f174a-82df-4611-a3df-e13255d97533

我想,以取代&&在上述网址.我的结果应该是:

http://myserver.com/_layouts/feed.aspx?xsl=4&web=%2F&page=dda3fd10-c776-4d69-8c55-2f1c74b343e2&wp=476f174a-82df-4611-a3df-e13255d97533

有人可以发给我代码来完成这项工作吗?

谢谢

Mic*_*all 114

查看我的NSString类别的HTML.以下是可用的方法:

// Strips HTML tags & comments, removes extra whitespace and decodes HTML character entities.
- (NSString *)stringByConvertingHTMLToPlainText;

// Decode all HTML entities using GTM.
- (NSString *)stringByDecodingHTMLEntities;

// Encode all HTML entities using GTM.
- (NSString *)stringByEncodingHTMLEntities;

// Minimal unicode encoding will only cover characters from table
// A.2.2 of http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
// which is what you want for a unicode encoded webpage.
- (NSString *)stringByEncodingHTMLEntities:(BOOL)isUnicode;

// Replace newlines with <br /> tags.
- (NSString *)stringWithNewLinesAsBRs;

// Remove newlines and white space from string.
- (NSString *)stringByRemovingNewLinesAndWhitespace;
Run Code Online (Sandbox Code Playgroud)


Chu*_*uck 14

[urlString stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
Run Code Online (Sandbox Code Playgroud)

  • @Abizern:许多语言都有内置的方法来编码和解码HTML实体,Obj-C缺乏这个以及程序员自2002年以来认为理所当然的许多其他东西.搜索和替换是一个糟糕的替代品,因为你将不得不花费相当长的时间知道你获得所有实体. (10认同)
  • @nbojja你想要多少内置?如果你担心的话,添加一个方法,在NSString上作为一个类别,然后内置它. (2认同)

Lou*_*arg 8

iPhone SDK中没有内置功能.您应该提交一个您想要该功能的错误.在普通的Mac OS X SDK中,您可以将片段作为HTML 加载到NSAttributedString中,并要求它回送普通字符串,或者使用CFXMLCreateStringByUnescapingEntities().

@interface NSString (LGAdditions)
- (NSString *) stringByUnescapingEntities;
@end

@implementation NSString (LGAdditions)
- (NSString *) stringByUnescapingEntities {
  CFStringRef retvalCF = CFXMLCreateStringByUnescapingEntities(kCFAllocatorDefault, (CFStringRef)self, NULL);
  return [NSMakeCollectable(retvalCF) autorelease];
}
@end
Run Code Online (Sandbox Code Playgroud)