views:

1937

answers:

4

Hi All,

I have a URL string in the following format.

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

I want to replace & with & in the above URL. My result should be:

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

Can someone post me the code to get this done?

Thanks

+5  A: 
[urlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
Chuck
I did the same...but is there any builtin way to do this...
nbojja
+3  A: 

There is no built-in function for this in the iPhone SDK. You should file a bug that you want the functionality. In the normal Mac OS X SDK you can either load the fragment into an NSAttributedString as HTML and ask it to hand back a plain string, or use CFXMLCreateStringByUnescapingEntities().

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

@implementation NSString (LGAdditions)
- (NSString *) stringByUnescapingEntities {
  CFStringRef retvalCF = CFXMLCreateStringByUnescapingEntities(kCFAllocatorDefault, (CFAllocatorRef)self, NULL);
  return [NSMakeCollectable(retvalCF) autorelease];
}
@end
Louis Gerbarg
+8  A: 

Check out my NSString category for HTML. Here are the methods available:

  • (NSString *)stringByConvertingHTMLToPlainText;
  • (NSString *)stringByDecodingHTMLEntities;
  • (NSString *)stringByEncodingHTMLEntities;
  • (NSString *)stringWithNewLinesAsBRs;
  • (NSString *)stringByRemovingNewLinesAndWhitespace;
Michael Waterfall
Thanks for this, Michael -- very handy! (As handy as the answer to this question that got accepted is wrong!)
Dave Peck
No problem ;) Glad you found it useful!
Michael Waterfall
A: 

Michael's answer is right! works like a charming! Great thanks man!

springrider