How to check if a character exists in a string with Swift

1 Answer

0 votes
import Foundation

// Define the string we want to search in
let s = "swiftlang"

// Check for a character using contains()
let hasS = s.contains("s")   // true
let hasZ = s.contains("z")   // false

// Print the raw boolean results
print(hasS)
print(hasZ)

// Conditional check
if s.contains("s") {
    print("exists")
} else {
    print("not exists")
}


/*
run:

true
false
exists

*/

 



answered 1 hour ago by avibootz
...