13

I want to decode HTML string and store it in NSString.

following is the html string for one of the response from google direction api.

teststring is Turn <b>right</b> onto <b>Kennington Park Rd/A3</b><div style="font-size:0.9em">Continue to follow A3</div><div style="font-size:0.9em">Entering toll zone in 1.7&nbsp;km at Newington Causeway/A3</div><div style="font-size:0.9em">Go through 2 roundabouts</div> 

I want to store this html string in Array of 4 different NSStrings with following 4 NSStrings (removing all information for size colour)

Turn right onto Kennington Park Rd/A3 Continue to follow A3 Entering toll zone in 1.7 km at Newington Causeway/A3 Go through 2 roundabouts 

I have used following method to convert html string to plain text.(html_response is the response from server and stringByConvertingHTMLToPlainText is the method defined in custom class file)

testString = (NSString*) [html_response stringByConvertingHTMLToPlainText]; NSLog(@"%@",testString); 

but it converts whole string instead of breaking it in parts. output at the console is

Turn right onto Kennington Park Rd/A3Continue to follow A3 Entering toll zone in 1.7 km at Newington Causeway/A3 Go through 2 roundabouts 

So do I need to write custom method to do this? Any help will be greatly appreciated.

1 Answer 1

18

There's a very nice NSString category that you can use, it's a part of the project MWFeedParser. More specifically, you look for the file NSString+HTML.

The NSString+HTML category adds the following methods to the NSString Class,

- (NSString *)stringByStrippingTags; - (NSString *)stringWithNewLinesAsBRs; - (NSString *)stringByRemovingNewLinesAndWhitespace; - (NSString *)stringByDecodingHTMLEntities; - (NSString *)stringByEncodingHTMLEntities; 

You could then try something like:

NSString *summary = [[[htmlString stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities]; 

Hope it helps!, good luck :)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer. by the method you suggested, I have html string is converted to nsstring but I need to store it as an array containing 4 different nsstring. Do you any idea how can I do it? Do I neeed to override above methods?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.