views:

24

answers:

1

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 *storyURL = [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.

A: 

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.

paxswill
Thanks. That worked. But instead of joining the string back I just used the first element of the brokenStrings array.
Aditya
That'll work in your example, but if you have a string like 'bananas\n\t in pajamas\n\t\t are walking down the stairs' you'll only get 'bananas' instead of 'bananas in pajamas are walking down the stairs'
paxswill
Very true. Do you have a generic solution for this kind of problem?
Aditya
Joining them back together with `componentsJoinedByString:` like in the example I gave should work.
paxswill