NSXMLParser adds extra characters to every string
I am creating a simple FeedReader iPhone app where a RSS is feed is parsed using NSXMLParser. The code to parse the RSS is a standard code similar to the one shown here.
But I noticed that every parsed element has the '\n\t\t' characters at the end. Ex.
link = "http://bakery.cakephp.org/articles/view/exporting-data-to-csv-the-cakephp-way\n\t\t\t";
date = "Fri, 23 Apr 2010 10:02:55 -0500\n\t\t\t";
Because of this, I cannot use the link to feed it to NSWebView and it has illegal characters. Even if I use something like
NSString *sto开发者_如何学JAVAryURL = [NSString stringWithFormat:@"%@", self.selectedURL];
NSString *webURL = [storyURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
the url becomes invalid. Ex.
http://bakery.cakephp.org/articles/view/creating-pdf-files-with-cakephp-and-tcpdf%0A%09%09%09
which obviously gives a 404 error.
Is there any way by which I can get rid of those extra characters at the end of every parsed string?
Thanks.
Those are control characters. In your example there's a newline, and then three tabs, probably for indentation. To strip them you can use a couple of methods from NSString
and NSArray
.
//This breaks the given string apart at the control characters
//This will find all of the control characters in the string
NSArray *brokenStrings = [dirtyString componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]];
//Now join them back together
NSString *cleanString = [brokenStrings componentsJoinedByString:@""];
When componentsSeparatedByCharactersInSet:
encounters one of the control characters, it is represented in the array by a blank string.
For some more guidance, a similar question was answered here, and this exact problem was discussed here, but in a more complicated manner.
精彩评论