我有一些来自JSON文件的文本,在本文中,我应用了utf8编码,但是这个编码器不识别非标准字符___,它是大写字符,有一个方法来净化MA字符串吗?
这是我的功能
func stringToUTF8String (stringaDaConvertire stringa: String) -> String {
let encodedData = stringa.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
//println(attributedString.string)
return attributedString.string
}
谢谢
最佳答案
我找到了解决办法,
utf8取8位的表ascii,而utf16取16位的ascii表,通过在
func stringToUTF16String (stringaDaConvertire stringa: String) -> String {
let encodedData = stringa.dataUsingEncoding(NSUTF16StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
//println(attributedString.string)
return attributedString.string
}
简单广告功能
关于string - swift UTF8编码和非UTF8字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27849966/