Different ways to check for String prefix in Swift

Swift String

Checking whether a String starts with a string #

You can use the hasPrefix(_:) method to test whether a string begins with the specified prefix.

let str = "Hello! Swift"
str.hasPrefix("Hello") // true

Getting a substring of prefix #

There are many ways and variations to do this.

Using length #

You use use prefix(_ maxLength:) to get substrings up to the specified length.

let str = "Hello! Swift"
str.prefix(6)
// "Hello!"

Since the parameter is maxLength, you can specify an argument which larger than a string without crashing. This method will return the whole string if the number specified is larger than the string length.

let str = "Hello! Swift"
str.prefix(100)
// "Hello! Swift"

Using String.Index #

String.Index is like a cursor to tell the position of a character in a string. In my example, I will point our String.Index to ! in Hello! Swift string.

To do that, we get the string startIndex and add the offset of 5.

let str = "Hello! Swift"
let index = str.index(str.startIndex, offsetBy: 5)
// index point to !

After we have the index, we have two options to get a substring of a prefix, prefix(through:) and prefix(upTo:).

The only difference between these two methods is prefix(upTo:) will return a prefix up to the string index specified, but not include the specified position.

let str = "Hello! Swift"
let index = str.index(str.startIndex, offsetBy: 5)

str.prefix(through: index) // "Hello!"
str.prefix(upTo: index) // "Hello", not include index specified (!)

Subscripts #

We can achieve the same result of prefix(through:) and prefix(upTo:) with a string subscript with a range of string index.

let str = "Hello! Swift"
let index = str.index(str.startIndex, offsetBy: 5)

str[...index] // "Hello!", same as str.prefix(through: index)
str[..<index] // "Hello", same as str.prefix(upTo: index)

Advanced #

All of the examples above are getting prefix by using a length and index of characters. If you have specific needs, you might want to use the following methods.

Condition #

You can use prefix(while:) to get a prefix up to the point where the predicate return false.

let str = "Hello! Swift"
str.prefix(while: { (character) -> Bool in
return character != " "
})
// Hello!

In the above example, we get a prefix until the first empty character. The result is everything before empty space, Hello!.

String.Index #

In the previous String.Index example, we use a simple index offset, but String.Index has many more ways to get an index of a character.

Here are some examples:

firstIndex(of:), this method will return the first index where the specified value appears.

let str = "Hello! Swift"
if let index = str.firstIndex(of: "l") {
str.prefix(through: index) // "Hel"
}

.lastIndex(of:), this method will return the last index where the specified value appears.

let str = "Hello! Swift"
if let index = str.lastIndex(of: "l") {
str.prefix(through: index) // "Hell"
}

After you get an index you want, you can use it as a argument for the methods we learn to get a prefix you want.


Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Tweet Share

If you enjoy this article, you can subscribe to the weekly newsletter.

Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.

← Home