The hasPrefix() method checks whether the string begins with the specified string or not.
Example
hasPrefix() Syntax
The syntax of the string hasPrefix() method is:
string.hasPrefix(str: String)
Here, string is an object of the String class.
hasPrefix() Parameters
The hasPrefix() method takes a single parameter:
- str - check whether string starts with str or not
hasPrefix() Return Value
The hasPrefix() method returns:
- true - if the string begins with the given string
- false - if the string doesn't begin with the given string
Note: The hasPrefix() method is case-sensitive.
Example 1: Swift string hasPrefix()
Output
true true true false false
Example 2: Using hasPrefix() With if...else
Output
Penned by Kris Some other artist