如何从字符串中删除html标记?
我有一个字符串,其中包含html代码如何从字符串中删除html标记?,html,ios,swift2,Html,Ios,Swift2,我有一个字符串,其中包含html代码 让htmlString=文本和文本 我想忽略html代码,只包含文本的字符串 谢谢。尝试使用如下css文件: html文件: <p id="myText" /> 尝试使用css文件,如下所示: html文件: <p id="myText" /> 您可以尝试使用-[NSAttributedString initWithData:options:documentAttributes:error:] + (NSAttributedStr
让htmlString=文本和文本
我想忽略html代码,只包含文本的字符串
谢谢。尝试使用如下css文件:
html文件:
<p id="myText" />
尝试使用css文件,如下所示:
html文件:
<p id="myText" />
您可以尝试使用-[NSAttributedString initWithData:options:documentAttributes:error:]
+ (NSAttributedString*)attributedStringForHTMLStrippingWithHTMLString:(NSString*)htmlString error:(NSError**)error
{
NSAttributedString *result = nil;
NSMutableAttributedString *attributedString = nil;
NSData *htmlStringData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
attributedString = [[NSMutableAttributedString alloc] initWithData:htmlStringData
options:options
documentAttributes:nil
error:error];
result = [attributedString copy];
return result;
}
+ (NSString*)stripStringOfHTMLTags:(NSString*)htmlString
{
NSString *result = nil;
NSError *error = nil;
NSAttributedString *attributedString = [self attributedStringForHTMLStrippingWithHTMLString:htmlString error:&error];
result = [attributedString string];
return result;
}
您可以尝试使用-[NSAttributedString initWithData:options:documentAttributes:error:]
+ (NSAttributedString*)attributedStringForHTMLStrippingWithHTMLString:(NSString*)htmlString error:(NSError**)error
{
NSAttributedString *result = nil;
NSMutableAttributedString *attributedString = nil;
NSData *htmlStringData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
attributedString = [[NSMutableAttributedString alloc] initWithData:htmlStringData
options:options
documentAttributes:nil
error:error];
result = [attributedString copy];
return result;
}
+ (NSString*)stripStringOfHTMLTags:(NSString*)htmlString
{
NSString *result = nil;
NSError *error = nil;
NSAttributedString *attributedString = [self attributedStringForHTMLStrippingWithHTMLString:htmlString error:&error];
result = [attributedString string];
return result;
}
您可以使用NSAttributedString
从字符串中删除html标记
请查找以下代码:
let htmlString = "<p style=\"text-align: right;\"> text and text"
do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
print("final strings :",attributedString.string)
} catch {
fatalError("Unhandled error: \(error)")
}
Swift 3.0-(Xcode 8.2)更新
并调用htmlString方法:
let yourHtmlString = "<p style=\"text-align: right;\"> text and text"
let decodedString = String(htmlString:yourHtmlString)
让您的HTMLSTRING=“文本和文本”
让decodedString=String(htmlString:yourHtmlString)
您可以使用NSAttributedString
从字符串中删除html标记
请查找以下代码:
let htmlString = "<p style=\"text-align: right;\"> text and text"
do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
print("final strings :",attributedString.string)
} catch {
fatalError("Unhandled error: \(error)")
}
Swift 3.0-(Xcode 8.2)更新
并调用htmlString方法:
let yourHtmlString = "<p style=\"text-align: right;\"> text and text"
let decodedString = String(htmlString:yourHtmlString)
让您的HTMLSTRING=“文本和文本”
让decodedString=String(htmlString:yourHtmlString)
你能举一个例子说明你想要什么样的输出吗?对于hello world
我只想要hello world给定html“”你想要最终结果是什么样的?是这样的吗?我认为提供更多的信息和一个代码示例或至少是伪代码示例会非常有帮助…
bold这是我希望在粗体结尾处显示的文本。
\r\n\r\n您想去掉标记并取出纯文本吗。不幸的是,它并不像可能的那么简单,因为HTML并不总是符合XML规则,但大多数现代HTML应该非常接近。首先,只需在字符串中一次循环一个字符。如果您在标记的“外部”,则将该字符添加到输出字符串中。如果你在标签内,不要这样做。天真地说,当你看到一个“”时,你就在一个标记的“内部”-尝试一下,看看你是怎么做的,但是可靠的文本提取在所有情况下都不是微不足道的。你能举个例子说明你想要什么样的输出吗?对于hello world
我只想要hello world给定html““你到底希望最终结果是什么样的?是这样的吗?我认为提供更多的信息和一个代码示例或至少是伪代码示例会非常有帮助…
bold这是我希望在粗体结尾处显示的文本。
\r\n\r\n您想去掉标记并取出纯文本吗。不幸的是,它并不像可能的那么简单,因为HTML并不总是符合XML规则,但大多数现代HTML应该非常接近。首先,只需在字符串中一次循环一个字符。如果您在标记的“外部”,则将该字符添加到输出字符串中。如果你在标签内,不要这样做。天真地说,当你看到一个“”时,你就在一个标记的“内部”——试试看你是怎么做的,但在所有情况下,可靠的文本提取都不是微不足道的。他是在向swift2提问。答案应该是swift2。他在问swift2的问题。答案应该在swift2中。虽然这是有效的,但仍有一些问题需要注意。它只在主线程上工作,如果在后台线程上使用它,可能会看到随机崩溃。如果你经常这么说的话,表现会很糟糕。这两种方法的原因都是它启动了一个UIWebView来执行转换。它只在主线程上工作,如果在后台线程上使用它,可能会看到随机崩溃。如果你经常这么说的话,表现会很糟糕。这两种方法的原因都是为了执行转换而启动UIWebView。