转换& 到&在Objective-C中

我有一个以下格式的URLstring。

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

我想replace&&在上面的url。 我的结果应该是:

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

有人可以发布我的代码来完成这个工作吗?

谢谢

4 Solutions collect form web for “转换& 到&在Objective-C中”

 [urlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; 

查看我的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; 

iPhone SDK中没有内置的function。 你应该提交一个你想要的function的bug 。 在正常的Mac OS X SDK中,您可以将片段作为HTML加载到NSAttributedString中 ,并要求它传回一个纯string,或使用CFXMLCreateStringByUnescapingEntities() 。

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

对于iOS,以下代码应该适用于数字代码。 这应该是相对容易的扩展到&amp;

 -(NSString*)unescapeHtmlCodes:(NSString*)input { NSRange rangeOfHTMLEntity = [input rangeOfString:@"&#"]; if( NSNotFound == rangeOfHTMLEntity.location ) { return input; } NSMutableString* answer = [[NSMutableString alloc] init]; [answer autorelease]; NSScanner* scanner = [NSScanner scannerWithString:input]; [scanner setCharactersToBeSkipped:nil]; // we want all white-space while( ![scanner isAtEnd] ) { NSString* fragment; [scanner scanUpToString:@"&#" intoString:&fragment]; if( nil != fragment ) { // eg '&#38; B' [answer appendString:fragment]; } if( ![scanner isAtEnd] ) { // implicitly we scanned to the next '&#' int scanLocation = (int)[scanner scanLocation]; [scanner setScanLocation:scanLocation+2]; // skip over '&#' int htmlCode; if( [scanner scanInt:&htmlCode] ) { char c = htmlCode; [answer appendFormat:@"%c", c]; scanLocation = (int)[scanner scanLocation]; [scanner setScanLocation:scanLocation+1]; // skip over ';' } else { // err ? } } } return answer; } 

一些unit testing代码…

 -(void)testUnescapeHtmlCodes { NSString* expected = @"A & B"; NSString* actual = [self unescapeHtmlCodes:@"A &#38; B"]; STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual ); expected = @"& B"; actual = [self unescapeHtmlCodes:@"&#38; B"]; STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual ); expected = @"A &"; actual = [self unescapeHtmlCodes:@"A &#38;"]; STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual ); } 
  • 在Objective C中dynamic调用一个类方法
  • 以编程方式调用故事板场景(无需继续)?
  • Xcode 4.2首选项“支持无线连接的设备”是做什么的?
  • iPhone:如何确定线程是否是主线程?
  • 为什么detailTextLabel不可见?
  • Sqlite文件位置核心数据
  • 如何从NSString获取单个NSString字符
  • 快速添加单引脚MKMapView?
  • parsingPlist(NSString)到NSDictionary
  • 在Objective-C中创build一个抽象类
  • 在Xcode 6中使用iOS 8扩展存档时出现警告