title: How to make sure that a string contains a whole word not just part of it
body: <p>I need some code to make sure that if a whole word exists in a return formatted text file it is accepted and that, if only part of it is present, it is not considered.</p>
  <p>If I type lau in the TextField it is accepted and I would rather the answer was false until a whole word is matched</p>
  <p>Here is the file limited.txt I use in my project. Each word is on a separate line:
  appetitive
  appetitiveness
  appetitost
  appetize
  appetized
  appetizement
  appetizer
  appetizers
  appetizing
  appetizingly
  appinite
  appius
  appl
  applanate
  applanation
  applaud
  applaudable
  applaudably
  applauded
  applauder
  applauders
  applauding
  applaudingly
  applauds
  applause
  applauses
  applausive
  applausively
  apple
  appleberry
  appleblossom
  applecart
  appled
  appledrane
  appledrone
  applegrower
  applejack
  applejohn
  applemonger</p>
  <p>Thanks for your help</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View{
      @ObservedObject var textFileStringContent: TexFileReader
      @State private var text = &quot;&quot;
  
      var body: some View{
          VStack {
              TextField(&quot;please type the word to check&quot;, text: $text)
              // so that it does not matter if user capitalises a word
              if textFileStringContent.data.contains(self.text.lowercased()) {
                  Text(&quot;part of it exists&quot;)
                  
                  // I tried to code it in here but to no avail
                  // if it is a whole word {
                  // Text(&quot;congratulations it does exist&quot;)
                  // }
                  
              } else if !text.isEmpty {
                  Text(&quot;sorry no such word&quot;)
              }
          }.padding().font(.headline)
              .navigationBarTitle(&quot;Word checker&quot;)
      }
  }
  
  class TexFileReader: ObservableObject {
      @Published var data: String = &quot;&quot;
      
      init() { self.load(file: &quot;limited&quot;) }
      func load(file: String) {
          if let filepath = Bundle.main.path(forResource: file, ofType: &quot;txt&quot;) {
              do {
                  let contents = try String(contentsOfFile: filepath)
                  DispatchQueue.main.async {
                      self.data = contents
                      
                       print(self.data.contains(&quot;lau&quot;))
                      // this prints true even if lau is not a whole word
                      // applaud
                      // applaudable
                      // applaudably
                      // applauded
                      // applauder
                      // applauders
                      // applauding
                      // applaudingly
                      // applauds
                      // applause
                      // applauses
                      // applausive
                      // applausively
                      // but present in each of these
                      // I need to make sure that the match is a whole word not just part of one
                     
                      
                  }
              } catch let error as NSError {
                  print(error.localizedDescription)
              }
          } else {
              print(&quot;File not found&quot;)
          }
      }
  }
  </code></pre>