如何在 UITextView 中替换句子中的确切关键字 [英] How to replace exact keyword in a sentence in UITextView

查看:29
本文介绍了如何在 UITextView 中替换句子中的确切关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,下面的代码不仅用 secondString 替换 firstString,而且将 secondString 放在句子前面.我的问题是如何替换这个 firstString?替换字符是正确的方法吗?

i got a problem where below codes not only replacing firstString with secondString but put secondString in front of a sentence. My question is how do i replace this firstString? is replacingCharacters is the right method?

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 的任何出现,那么你应该使用: replacingOccurrences(of:with:) 像这样:

If you just need to replace any occurrences of your firstString then you should use: replacingOccurrences(of:with:) like so:

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

您遇到第二个字符串在句子前面的问题的原因是因为在您的替换方法中:

and the reason why you are getting the problem where secondString is in front of a sentence is because in your replace method:

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 的范围:

you are trying to get the range of firstString:

let range = firstString.startIndex..<firstString.endIndex

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

and attempting to apply that range to your textview's string which is a totally different string from firstString therefore the range you are supplying is not what you are intending. Your firstString and textview text are different therefore they are going to have different ranges, it's not good to use range values of one string with another string

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

If you really want to replace the string by finding/using the range, then you need to first detect what's is the range you want to replace the text in, that means in your textview text which is

"this (firstString) 是一个句子的例子"

"this (firstString) is an example of a sentence"

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

you'll need to find the range firstString in that sentence by replacing:

let range = firstString.startIndex..<firstString.endIndex

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

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

the func range(of searchString: String) -> NSRange will "Finds and returns the range of the first occurrence of a given string within the receiver." according the documentation from Apple: https://developer.apple.com/documentation/foundation/nsstring/1410144-range

这篇关于如何在 UITextView 中替换句子中的确切关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆