2017-09-26 77 views
1

我遇到了一个问题,下面的代码不仅用firstString替换secondString,而且把secondString放在句子前面。我的问题是我如何替换这个firstString?替换字符是正确的方法?如何替换UITextView中的句子中的确切关键字

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() 
    } 

} 

回答

4

如果你只需要替换你的firstString的任何事件,然后y OU应该使用: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文本“这(firstString)是一个句子的一个例子”

你需要在该句子中找到范围firstString,取代:

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

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

func range(of searchString: String) -> NSRange将“查找并返回接收器内的给定的字符串的第一个匹配的范围内。”根据苹果的文件:https://developer.apple.com/documentation/foundation/nsstring/1410144-range

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 

    } 

} 
2

最简单的方式来实现自己在做什么,

textview.text?.replacingOccurrences(of: firstString, with: secondString) 
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() 
    } 
} 
相关问题