IOS removes HTML tags leaving the original string

Source: Internet
Author: User

Do development sometimes will encounter the data returned by the server is HTML, we can choose to use WebView to load the way to deal with, of course, this method does not apply to all scenarios, usually we will choose to remove the unnecessary HTML tags to leave the necessary text information, now take a look at it.

First, provide a string of HTML tags

   @" <p><span style=\ "font-family: Song body"> Labor is the activity of human beings to create material or spiritual wealth, physical and mental. We are laborers and witnesses to the work of others. Labor exists in the present and also in the past and in the future. Labor people have different, the object of labor has different, the way of Labor has different, the environment of Labor has different </span>......<span style=\ "font-family: Song body" > So, the taste of labor also has different. Labor is very important, taste the taste of labor is also very important. How do you experience or feel about labor? Please take </span> "<span style=\" font-family: Song body \ "> Taste Labor </span>" <span style=\ "font-family: Arial" > In order to write an article not less than </span>800<span style=\ "font-family: Song body \" > Writing composition, in addition to poetry, not limited to the style. </span></p><p><br/></p>";

Then provide a method

-(NSString *) filterhtml: (NSString *) html{Nsscanner* Scanner =[Nsscanner scannerwithstring:html]; NSString* Text =Nil;  while([Scanner isatend]==NO) {[Scanner scanuptostring:@"<"Intostring:nil]; [Scanner scanuptostring:@">"intostring:&text]; HTML= [HTML stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@"%@>", text] withstring:@""]; }    returnhtml;}

When you're done, you get the information you need, not the HTML tags.

So it's over. No, now let's take a look at the usage of Nsscanner.

Nsscanner is a class that scans a string for a specified character, translates it into a string or number we need, and the core is the movement of the position, i.e. the scanlocation movement.

In the previous method, the object to be scanned is first indicated by HTML (nsstring) NSString * Text is important to save the string we want to scan into text.

And this scan to the string is the string scanuptostring before > This means that the scanlocation is stuck in the > before the string passed to text.

Look back and see how we're going to get rid of HTML tags. The entire process is performed during the scanning process Nsscanner when the Scanuptostring method is executed, once it is scanned to the desired string such as "<" in the example, Its scanlocation becomes the initial position of the HTML. So, to replace the HTML tag with an empty string after performing a complete scan, the next scan, that is, the HTML string in the while the label characters will be less, and the initial position of each scan is not changed at the end of the last scan, that is, "<" The front of the label.

Reference links for a better introduction to Nsscanner http://blog.csdn.net/likendsl/article/details/7974761

IOS removes HTML tags leaving the original string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.