我想在文字中过滤双引号并与单词分开。但是当我用双引号添加双引号时(比如这"""),它没有用。我尝试用这种方式添加(" \"")在双引号之间,但它也没有用。
我的代码如下。你可以在FindCommonWords函数中看到我想要的东西。
我该如何解决这个问题?
感谢。
import UIKit
import ROGoogleTranslate
import SwiftSoup
class FeedDetailVC: UIViewController, UIWebViewDelegate {
@IBOutlet weak var txtMain: UITextView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var lblHeader: UILabel!
var commonWordsArray = [String]()
var selectedHeader = String()
override func viewDidLoad() {
super.viewDidLoad()
lblHeader.text = selectedHeader
self.txtMain.sizeToFit()
DispatchQueue.main.async {
self.contentHeight = self.lblHeader.frame.height + self.txtMain.frame.height
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.contentHeight + 100)
self.reloadInputViews()
}
findCommonWords()
}
func findCommonWords() {
let textViewText = txtMain.text
var commonWords = textViewText?.lowercased().components(separatedBy: [" ",".",",","=","(",")","-","/",":","\""]).filter({ !$0.isEmpty })
let rejectedWords: [String] = ["the","on","in","a","an","of","off","up","and","to","are","is","am","—","se","iphone","was","it","at","\""]
for index in rejectedWords {
commonWords = commonWords?.filter{ $0 != index}
}
let wordCounts = NSCountedSet(array: commonWords!)
let sortedWords = wordCounts.allObjects.sorted(by: {wordCounts.count(for: $0) > wordCounts.count(for: $1) }) [0..<5]
yPoint = self.txtMain.frame.height + 15
for word in sortedWords {
//print("\(word) \(wordCounts.count(for: word)) times")
let sortedWordsLabel = UILabel()
sortedWordsLabel.frame = CGRect(x: 0, y: 0, width: 120, height: 25)
sortedWordsLabel.center = CGPoint(x: 80, y: yPoint)
sortedWordsLabel.text = word as? String
sortedWordsLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 15)
sortedWordsLabel.textAlignment = .left
self.scrollView.addSubview(sortedWordsLabel)
yPoint += 25
commonWordsArray.append(word as! String)
}
}
}
答案 0 :(得分:0)
“字符串文字可以包含以下特殊字符:
转义的特殊字符
\0
(空字符),\\
(反斜杠),\t
(水平标签),\n
(换行符),{{1} }(回车),\r
(双引号)和\"
(单引号)“
摘录自:Apple Inc.“The Swift Programming Language(Swift 2.1)。”iBooks。 https://itunes.apple.com/ca/book/the-swift-programming-language-swift-3-1/id881256329?mt=11