The startsWith() method of String class is used for checking prefix of a String. It returns a boolean value true or false based on whether the given string begins with the specified letter or word. For example: String str = "Hello"; //This will return true because string str starts with "He" str.
To check if a String str1 starts with another String str2 in Swift, use String. starts() function. Call starts() function on str1 and pass str2 as argument. starts() function returns a boolean value.
Returns: A boolean value: true - if the string starts with the specified character(s)
startsWith method is case sensitive.
use hasPrefix
instead of startsWith
.
Example:
"hello dolly".hasPrefix("hello") // This will return true
"hello dolly".hasPrefix("abc") // This will return false
here is a Swift extension implementation of startsWith:
extension String {
func startsWith(string: String) -> Bool {
guard let range = rangeOfString(string, options:[.AnchoredSearch, .CaseInsensitiveSearch]) else {
return false
}
return range.startIndex == startIndex
}
}
Example usage:
var str = "Hello, playground"
let matches = str.startsWith("hello") //true
let no_matches = str.startsWith("playground") //false
To answer specifically case insensitive prefix matching:
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
return lowercased().hasPrefix(prefix.lowercased())
}
}
or:
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
return lowercased().starts(with: prefix.lowercased())
}
}
note: for an empty prefix ""
both implementations will return true
range(of:options:)
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
return range(of: prefix, options: [.anchored, .caseInsensitive]) != nil
}
}
note: for an empty prefix ""
it will return false
extension String {
func caseInsensitiveHasPrefix(_ prefix: String) -> Bool {
guard let expression = try? NSRegularExpression(pattern: "\(prefix)", options: [.caseInsensitive, .ignoreMetacharacters]) else {
return false
}
return expression.firstMatch(in: self, options: .anchored, range: NSRange(location: 0, length: characters.count)) != nil
}
}
note: for an empty prefix ""
it will return false
Edit: updated for Swift 3.
The Swift String class does have the case-sensitive method hasPrefix()
, but if you want a case-insensitive search you can use the NSString method range(of:options:)
.
Note: By default, the NSString methods are not available, but if you import Foundation
they are.
So:
import Foundation
var city = "San Antonio"
var searchString = "san "
let range = city.range(of: searchString, options:.caseInsensitive)
if let range = range {
print("San Antonio starts with san at \(range.startIndex)");
}
The options can be given as either .caseInsensitive
or [.caseInsensitive]
. You would use the second if you wanted to use additional options, such as:
let range = city.range(of: searchString, options:[.caseInsensitive, .backwards])
This approach also has the advantage of being able to use other options with the search, such as .diacriticInsensitive
searches. The same result cannot be achieved simply by using . lowercased()
on the strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With