【问题标题】:How to replace exact keyword in a sentence in UITextView如何在 UITextView 中替换句子中的确切关键字
【发布时间】:2017-09-26 03:07:59
【问题描述】:

我遇到了一个问题,下面的代码不仅将 firstString 替换为 secondString,而且将 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()
    }

}

【问题讨论】:

    标签: ios swift string uitextview


    【解决方案1】:

    如果您只需要替换出现的任何 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 文本中是

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      实现你正在做的事情的最简单方法,

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:
          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()
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2020-05-17
            • 1970-01-01
            • 2021-07-30
            • 2014-12-21
            • 2015-10-20
            • 2021-09-18
            • 2016-10-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多