使用 Swift 删除一组特定字符之间的所有内容 [英] Removing everything between a certain set of characters with Swift

查看:32
本文介绍了使用 Swift 删除一组特定字符之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Swift 和本机编程很陌生,对于我自己做的一个小项目,我在 twitter 搜索后获得了完整的 html,我试图过滤掉文本第一条推文.我已经能够获得第一条推文,包括其中的所有标签,但我对如何仅过滤文本并删除 HTML 元素有点无能为力.

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

请耐心等待,因为我对此很陌生,我知道我什至可能根本没有做对,并且有一种更简单的方法来拉一条推文,而不是所有这些麻烦.如果是这样,也请告诉我.

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

func html2String(html:String) ->细绳 {返回 NSAttributedString(data: html.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.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 扩展

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

或 NSData 作为函数:

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

用法:

"

测试

//让我们将此 html 加载为字符串

导入 UIKit类视图控制器:UIViewController {让 questionLink = "http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573"覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.如果让 questionUrl = NSURL(string: questionLink) {println("正在加载网址")如果让 myHtmlDataFromUrl = NSData(contentsOfURL: questionUrl){println(myHtmlDataFromUrl.htmlString)}}}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()//处理任何可以重新创建的资源.}}

I'm quite new to Swift and native programming, and for a small project I'm doing for myself I'm getting in the full html after doing a twitter search, and I'm trying to filter out just the text of the first tweet. I'm up to the point were I'm able to get the first tweet, including all the tags that are in there, but I'm a bit clueless on how to filter just the text out of there and remove the HTML elements.

For example, it's pretty easy to take a single tweet and filter out the possible <a href=""> and <span> etc. But when I'd change the tweet or search, it wouldnt work as specific. The thing I'm looking for really is on how to remove everything in a string that starts with < and ends with >. This way I'm able to filter out all the stuff I don't need in my string. I'm using "string.componentsSeparatedByString()" to grab the one tweet I need out of all the HTML, but I can't use this method to filter all the stuff out of my string.

Please bear with me since I'm quite new at this, I'm aware that I'm possibly not even doing this right at all and there's a way easier method to pull a single tweet instead of all this hassle. If so, please let me know as well.

解决方案

You can create a function to do it for you as follow:

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

or as an extension:

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)!
    }
}

you might prefer as a NSData extension

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

or NSData as a function:

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

Usage:

"<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 !!!"

// lets load this html as 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.
    }
}

这篇关于使用 Swift 删除一组特定字符之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆