我遇到了一个问题,下面的代码不仅用secondString替换firstString,而且在句子前面放了secondString。我的问题是如何替换这个firstString?替换字符是正确的方法吗?
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
let firstString: String = "xxx"
let secondString: String = "yyy"
override func viewDidLoad() {
super.viewDidLoad()
textView.text = "this \(firstString) is an example of a sentence"
}
func replace() {
var finalString: String?
let range = firstString.startIndex..<firstString.endIndex
print(firstString[range])
finalString = textView.text.replacingCharacters(in: range, with: secondString)
textView.text = finalString
}
@IBAction func replaceButton(_ sender: Any) {
replace()
}
}
答案 0 :(得分:4)
如果您只需要替换firstString的任何匹配项,那么您应该使用:replacingOccurrences(of:with:)
,如下所示:
textView.text.replacingCharacters(of: firstString, with: secondString)
以及你遇到secondString在句子前面的问题的原因是因为在你的替换方法中:
func replace() {
var finalString: String?
let range = firstString.startIndex..<firstString.endIndex
print(firstString[range])
finalString = textView.text.replacingCharacters(in: range, with: secondString)
textView.text = finalString
}
你正试图获得firstString的范围:
let range = firstString.startIndex..<firstString.endIndex
并尝试将该范围应用于textview的字符串,这是与firstString完全不同的字符串,因此您提供的范围不是您想要的范围。您的firstString
和textview文本不同,因此它们将具有不同的范围,将一个字符串的范围值与另一个字符串一起使用是不合适的
修改强> 如果你真的想通过查找/使用范围来替换字符串,那么你需要首先检测你想要替换文本的范围是什么,这意味着在你的textview文本中
&#34;这个(firstString)是一个句子的例子&#34;
您需要通过替换来找到该句子中的范围firstString
:
let range = firstString.startIndex..<firstString.endIndex
与
let range = textView.text.range(of: firstString)
func range(of searchString: String) -> NSRange
将&#34;查找并返回接收器中给定字符串第一次出现的范围。&#34;根据Apple的文件:https://developer.apple.com/documentation/foundation/nsstring/1410144-range
答案 1 :(得分:2)
实现目标的最简单方法,
textview.text?.replacingOccurrences(of: firstString, with: secondString)
答案 2 :(得分:1)
如果您想将字替换为另一个字,那么您应该使用replacingOccurrences(of:with:)
方法:
返回一个新字符串,其中出现所有目标字符串 接收器被另一个给定的字符串替换。
举个例子:
let originalString = "I live in Paris"
let updatedString = originalString.replacingOccurrences(of: "Paris", with: "New-York")
print(updatedString) // Prints "I live in New-York"
在你的情况下,你会有类似的东西:
class ViewController: UIViewController {
// MARK: @IBOutlets
@IBOutlet weak var textView: UITextView!
// MARK: Properties
let firstString: String = "pink"
let secondString: String = "blue"
// MARK: Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
textView.text = "The sky is \(firstString)"
print(textView.text) // Prints "The sky is pink"
}
// MARK: User Interaction
@IBAction func replaceButton(_ sender: Any) {
let originalString = textView.text
let updatedString = originalString.replacingOccurrences(of: firstString, with: secondString)
print(updatedString) // Prints "The sky is blue"
// Now you can set your text view's text to be equal to your updated string if you want :
textView.text = updatedString
}
}
答案 3 :(得分:0)
class ViewController: UIViewController
{
@IBOutlet weak var textView: UITextView!
let firstString: String = "xxx"
let secondString: String = "yyy"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.text = "this \(firstString) is an example of a sentence"
}
func replace() {
var finalString: String?
let originalString = textView.text
finalString = originalString?.replacingOccurrences(of:
firstString, with: secondString)
textView.text = finalString
}
@IBAction func replaceButton(_ sender: Any) {
replace()
}
}