Andrew K
Andrew K

Reputation: 1599

Apple Swift - Check to see if a character is present in a string

Python:

myString = "hello python"
if "p" in myString:
    print "true"

Swift:

var myString = "is Swift really all it's cracked up to be?"
if myString.rangeOfString("c") != nil{
    println("true")
}

Is this really the "swiftest" (easiest) way to check to see if a character is present in a string?

Upvotes: 0

Views: 511

Answers (1)

rintaro
rintaro

Reputation: 51911

If you want to find Character, you can use builtin contains function:

if contains(myString, "c") {
    println("true")
}

If you want to find substring, .rangeOfString("substring") != nil is easiest.

Upvotes: 1

Related Questions