title: Swift 4 Replace word within String
body: <p>The setup: A UITextField and a Tableview with suggested users
  I try to have the following result:</p>
  
  <p>I want users to be able to link other users.
  Its working fine as long as I search with my last word in the array</p>
  
  <pre><code> let caption = captionTextView.text
      let words = caption?.components(separatedBy: .whitespacesAndNewlines)
  
       guard let searchingWord = words?.last else {return}
  
      if searchingWord.hasPrefix("@") {
          self.indicator.startAnimating()
          let search = searchingWord.trimmingCharacters(in: CharacterSet.punctuationCharacters).lowercased()
   }
  </code></pre>
  
  <p>But in case a user wants to adjust a username in the middle or at least not at the end of the array, the searching functions doesn't work properly as it still searches with the last word in the array</p>
  
  <p>Example:</p>
  
  <pre><code>"Hey how are you @Lisa @Marcel @Thomas"
  </code></pre>
  
  <p>In case a user wants to change "@Lisa" to "@Lisbeth" the search function  will search with Thomas as its the last word in the array</p>
  
  <p>I wasn't able to get the word I am working at, only last and first words in the array, however I am able to get the current cursor location with</p>
  
  <pre><code>let cursor =  captionTextView.cursorOffset!
  </code></pre>
  
  <p>which is an extension.</p>
  
  <p>So how do I get the word I am working at up until the next "@" to the left und the next blank space to the right? Thanks in advance!</p>