Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check what a String starts with (prefix) or ends with (suffix) in Swift

Tags:

string

ios

swift

I'm trying to test if a Swift string starts or ends with a certain value. These methods do not exist:

var str = "Hello, playground" str.startsWith("Hello") // error str.endsWith("ground") // error 

I would also like to get the prefix and suffix strings. I could find a substring, as is answered here and here, but ranges are such a pain in Swift.

Is there an easier way to do it?

(I stumbled across the answer when I was reading the documentation, and since an SO answer didn't come up for my search terms, I am adding my Q&A here.)

like image 944
Suragch Avatar asked Oct 06 '15 10:10

Suragch


People also ask

How do you check if a string starts with a substring in Swift?

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.

How do you check if a string has a suffix?

The Java String class endsWith() method checks if this string ends with a given suffix. It returns true if this string ends with the given suffix; else returns false.

Does Swift have a prefix?

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.


2 Answers

Updated for Swift 4

Checking what a String starts with and ends with

You can use the hasPrefix(_:) and hasSuffix(_:) methods to test equality with another String.

let str = "Hello, playground"  if str.hasPrefix("Hello") { // true     print("Prefix exists") }  if str.hasSuffix("ground") { // true     print("Suffix exists") } 

Getting the Actual Prefix and Suffix Substrings

In order to get the actual prefix or suffix substring, you can use one of the following methods. I recommend the first method for it's simplicity. All methods use str as

let str = "Hello, playground" 

Method 1: (Recommended) prefix(Int) and suffix(Int)

let prefix = String(str.prefix(5)) // Hello let suffix = String(str.suffix(6)) // ground 

This is the better method in my opinion. Unlike the methods 2 and 3 below, this method will not crash if the indexes go out of bounds. It will just return all the characters in the string.

let prefix = String(str.prefix(225)) // Hello, playground let suffix = String(str.suffix(623)) // Hello, playground 

Of course, sometimes crashes are good because they let you know there is a problem with your code. So consider the second method below as well. It will throw an error if the index goes out of bounds.

Method 2: prefix(upto:) and suffix(from:)

Swift String indexes are tricky because they have to take into account special characters (like emoji). However once you get the index it is easy to get the prefix or suffix. (See my other answer on String.Index.)

let prefixIndex = str.index(str.startIndex, offsetBy: 5) let prefix = String(str.prefix(upTo: prefixIndex)) // Hello  let suffixIndex = str.index(str.endIndex, offsetBy: -6) let suffix = String(str.suffix(from: suffixIndex)) // ground 

If you want to guard against going out of bounds, you can make an index using limitedBy (again, see this answer).

Method 3: subscripts

Since String is a collection, you can use subscripts to get the prefix and suffix.

let prefixIndex = str.index(str.startIndex, offsetBy: 5) let prefix = String(str[..<prefixIndex]) // Hello  let suffixIndex = str.index(str.endIndex, offsetBy: -6) let suffix = String(str[suffixIndex...]) // ground 

Further Reading

  • Strings and Characters documentation
like image 73
Suragch Avatar answered Oct 06 '22 13:10

Suragch


Prefix and Suffix Equality

To check whether a string has a particular string prefix or suffix, call the string’s hasPrefix(:) and hasSuffix(:) methods, both of which take a single argument of type String and return a Boolean value.

Apple Doc

like image 24
Gobi M Avatar answered Oct 06 '22 14:10

Gobi M