使用Swift删除某些字符集之间的所有内容

时间:2014-12-26 20:36:40

标签: string swift

我对Swift和原生编程都很陌生,对于我正在为自己做的一个小项目,我在进行推特搜索后进入完整的html,我试图过滤掉文本第一条推文。我能够得到第一条推文,包括那里的所有标签,但我对如何过滤那里的文本并删除HTML元素感到有点无能为力。

例如,单个推文并过滤掉可能的<a href=""><span>等非常容易。但是当我更改推文或搜索时,它不会具体。我正在寻找的是如何删除以&lt;开头的字符串中的所有内容。以&gt;结尾这样我就可以过滤掉我在字符串中不需要的所有东西。我正在使用“string.componentsSeparatedByString()”从所有HTML中获取我需要的一条推文,但我不能使用此方法来过滤掉我的字符串中的所有内容。

请相信我,因为我对此很陌生,我知道我可能根本就没有做到这一点,并且有一种更方便的方法来拉一条推文而不是所有这些麻烦。如果是这样,请告诉我。

2 个答案:

答案 0 :(得分:4)

您可以创建一个函数来为您完成,如下所示:

func html2String(html:String) -> String {
    return NSAttributedString(data: html.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}

或作为扩展名:

extension String {
    var html2String:String {
        return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
    }
    var html2NSAttributedString:NSAttributedString {
        return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!
    }
}

您可能更喜欢NSData扩展

extension NSData{
    var htmlString:String {
        return  NSAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
    }
}

或NSData作为函数:

func html2String(html:NSData)-> String {
    return  NSAttributedString(data: html, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}

用法:

"<div>Testing<br></div><a href=\"http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573\"><span>&nbsp;Hello World !!!</span>".html2String  //  "Testing\n Hello World !!!"

let result = html2String("<div>Testing<br></div><a href=\"http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573\"><span>&nbsp;Hello World !!!</span>")  //  "Testing\n Hello World !!!"

//让我们将这个html加载为String

import UIKit

class ViewController: UIViewController {
    let questionLink = "http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573"
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let questionUrl = NSURL(string: questionLink) {
            println("LOADING URL")
            if let myHtmlDataFromUrl = NSData(contentsOfURL: questionUrl){
                println(myHtmlDataFromUrl.htmlString)
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

答案 1 :(得分:0)

在过去的几年中,Swift中的很多值都发生了变化,所以我只想发布Leo Dabus的答案的更新版本,更新为当前的Swift语法。

extension String {

    func removeHTMLEncoding() throws -> String? {
        guard let data = self.data(using: .utf8) else { return nil }
        let attr = try NSAttributedString(
            data: data,
            options: [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: NSNumber(value: String.Encoding.utf8.rawValue)
            ],
            documentAttributes: nil
        )
        return attr.string
    }

}

Kinda烦人的是,您仍然需要将字符串编码值转换为NSNumber-NSAttributedString已经过时了!