Find substring, Swift code to check subString in a string.


var string = "hello Swift"

if string.rangeOfString("Swift") != nil{
    println("exists")
}

// alternative: not case sensitive
if string.lowercaseString.rangeOfString("swift") != nil {
    println("exists")
}

  
        let string2 = "Hello.World"
        let needle: Character = "."
        if let idx = find(string2, needle) {
            let pos = distance(string2.startIndex, idx)
            println("Found \(needle) at position \(pos)")
        }
        else {
            println("Not found")
        }

Leave a Reply