How to compare two strings in swift?

Viewed 8647

I have the following problem:

I want to compare if one string contains "Schreibe etwas..."

I solved it with the following code:

selectedText = postTextView.text!
let isText = selectedText

if isText != "Schreibe etwas..." {
    shareButton.isEnabled = true
    abortButton.isEnabled = true
}

But its not working. How to solve this problem?

Thanks in advance for your help!

3 Answers

You can try

if !isText.contains("Schreibe etwas...") {
    shareButton.isEnabled = true
    abortButton.isEnabled = true
}
let myString =  isText.absoluteString
if myString.range(of:"Schreibe etwas") != nil { 

//do some thing

}

use this code to compare two String

  var a : String = "Schreibe etwas..."
  var b  : String = "Schreibe etwas..."

  if(a.caseInsensitiveCompare(b) == .orderedSame){
      print("string is Same")
  }
Related