ma11hew28
ma11hew28

Reputation: 126357

How do I check if a String includes a specific Character?

How do I check if a String includes a specific Character?

For example:

if !emailString.hasCharacter("@") {
    println("Email must contain at sign.")
}

Upvotes: 3

Views: 2508

Answers (3)

saurabh
saurabh

Reputation: 6775

You can use this

if emailString.rangeOfString("@") == nil {
        println("Email must contain at sign.")
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You can use the free-standing find function, like this:

let s = "hello"
if (find(s, "x") != nil) {
    println("Found X")
}
if (find(s, "l") != nil) {
    println("Found L")
}

Upvotes: 4

Daniel Klöck
Daniel Klöck

Reputation: 21137

Here you go:

if emailString.rangeOfString("@") != nil{
    println("@ exists")
}

Upvotes: 0

Related Questions