The hasPrefix()
method of a string instance in Swift checks if a string begins with a specified prefix. If true, it returns true
. Otherwise, it returns false
.
string.hasPrefix(prefix)
prefix
: This is the sub-string we want to check to see if it begins the string, string
.
This method returns a Boolean value. It returns a true
value if the prefix string prefix
begins the string string
. Otherwise, it returns a false
value.
// Create stringslet str1 = "Edpresso is the best"let str2 = "Best place to be"let str3 = "Learn all you need"// Check if the following prefixes existprint(str1.hasPrefix("Edpresso"))print(str2.hasPrefix("great"))print(str3.hasPrefix("Learn"))