AJ學IOS之去除服務器返回數據中的html標籤,去除指定字符串,替換字符串

AJ分享,必須精品html

 

一:問題

這裏寫圖片描述

如圖中,服務器返回的數據裏面有大串的html 可是咱們只用字符串,因爲不想麻煩後臺修改數據。。。。(喵很爲別人着想)因而本身想辦法解決。面試

其實解決的方法不少不少。。好比用字符串的截取方法的到range,而後根據位置來獲得裏面的想要的東東。。嘎的,想一想都崩潰。
還有呢用正則表達式等等。。。正則表達式,說實話這東西除了面試時候說說和學習時候用過作項目還歷來沒有本身寫過,pass,因而網上搜索學習,獲得了一個方法,共享給你們。正則表達式

二:解決

//去掉html標籤
-(NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html=[html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    return html;
}

恩,就是上面的方法,他會把那些標籤方法(帶着< >的)直接替換成了@」」 空格,而後返還回來的就是了。服務器

相關文章
相關標籤/搜索