如何在UITextView中替换句子中的确切关键字

我遇到一个问题,下面的代码不仅用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() } } 

如果你只需要替换你的firstString的任何出现,那么你应该使用:replacementOccurrences replacingOccurrences(of:with:)如下所示:

 textView.text.replacingCharacters(of: firstString, with: secondString) 

你之所以遇到secondString在句子前面的问题是因为在你的replace方法中:

 func replace() { var finalString: String? let range = firstString.startIndex.. 

你试图获得firstString的范围:

 let range = firstString.startIndex.. 

并尝试将该范围应用于textview的字符串,这是一个与firstString完全不同的字符串,因此您提供的范围不是您想要的范围。 你的firstString和textview文本是不同的,因此它们将具有不同的范围,将一个字符串的范围值与另一个字符串一起使用是不好的

编辑:如果你真的想通过查找/使用范围来替换字符串,那么你需要首先检测你想要替换文本的范围是什么,这意味着你的textview文本是

“this(firstString)是一个句子的例子”

你需要通过替换来找到该句子中的范围firstString

 let range = firstString.startIndex.. 

 let range = textView.text.range(of: firstString) 

func range(of searchString: String) -> NSRange将“查找并返回接收器中给定字符串第一次出现的范围”。 根据Apple的文档: https : //developer.apple.com/documentation/foundation/nsstring/1410144-range

实现目标的最简单方法,

 textview.text?.replacingOccurrences(of: firstString, with: secondString) 

如果你想用另一个单词替换一个单词 ,那么你应该选择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 } } 
 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() } }