Which method would you use to determine whether a certain substring is a suffix?

Which method would you use to determine whether a certain substring is a suffix?

Waldo helps you spend more time developing, less time testing by automating functional, E2E tests.

Get Started for Free

Sponsor sarunw.com and reach thousands of iOS developers.

Checking whether a String ends with a string

You can use the hasSuffix(_:) method to test whether a string ends with the specified string.

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

Getting a substring of a suffix

There are many ways and variations to do this.

Using length

You use use suffix(_ maxLength:) to get substrings up to the specified length, starting from the end of the string and count backward.

let str = "Hello! Swift"
str.suffix(5)
// "Swift"

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.suffix(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 can get a substring of a suffix using suffix(from:), which will return a substring from the specified position to the end of the collection (include the specified index).

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

str.suffix(from: index) // "! Swift", include the specified index (!)

Subscripts

We can achieve the same result of suffix(from:) with a string subscript with a range of string indexes.

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

str[index...] // "! Swift", same as str.suffix(from: index)

Using String.Index (From the back)

You can specify index by offset from the back, which might feel more natural than offset from the beginning.

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

str.suffix(from: index) // "Swift"

There is a difference between String property .startIndex and .endIndex that you should be aware of. The startIndex property will return the position of the first character of a String. The endIndex property will return the position after the last character in a String.

As a result, the endIndex property isn't a valid argument for some string's substring methods.

The endIndex work for suffix(from:) and other methods that the endIndex is not use to retrieve character, .e.g, .prefix(upTo:).

let str = "Hello! Swift"
let index = str.index(str.endIndex, offsetBy: 0) // or just str.endIndex

str.suffix(from: index) // "", empty string
str[index...] // "", emprt string
str.prefix(upTo: index) // "Hello! Swift"

But it won't work for a method in which the specified index is used to retrieve the character.

let str = "Hello! Swift"
let index = str.endIndex

str[index] // Fatal error: String index is out of bounds <1>
str.prefix(through: index) // Fatal error: String index is out of bounds <2>

<1> Retrieve character at end index, which is invalid.
<2> Return a substring from the start to the end index, which is invalid.

So, be mindful when using the endIndex.

Using String.Index (Advanced)

All of the examples above are getting suffix by using a length and index of characters. If you have specific needs, you have more sophisticated ways to get the 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.suffix(from: index) // "llo! Swift"
}

.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.suffix(from: index) // "lo! Swift"
}

.firstIndex(where:), returns the first index in which an element of the collection satisfies the given predicate.

In the following example, we find the first index of a character that isn't alphanumeric, which is ! in this case.

let str = "Hello! Swift"

let index = str.firstIndex { (character) -> Bool in
if let unicodeScalar = character.unicodeScalars.first, character.unicodeScalars.count == 1 {
return CharacterSet.alphanumerics.inverted.contains(unicodeScalar)
}

return false
}

if let index = index {
str.suffix(from: index) // "! Swift"
}

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

Which method would you use to determine whether a certain substring is a suffix?

Waldo helps you spend more time developing, less time testing by automating functional, E2E tests.

Get Started for Free

Sponsor sarunw.com and reach thousands of iOS developers.

  • Different ways to check for String prefix in Swift
  • Swift Strings and Characters
  • Apple Documentation
    • hasSuffix(_:)
    • suffix(_:)
    • suffix(from:)
    • firstIndex(where:)

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.

Which method would you use to determine whether a certain substring is the suffix of a string?

The EndsWith method compares the value parameter to the substring at the end of this string and returns a value that indicates whether they are equal.

Which method would you use to determine whether a certain substring is present in a string Python?

Use the in keyword to check if the substring is present in the string in the first place. The in keyword returns a Boolean value – a value that is either True or False . The in operator returns True when the substring is present in the string. Using the in keyword is a helpful first step before using the find() method.

Which of the following string methods can be used to determine if a string contains?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise.

Which method could be used to strip specific characters from the end of a string?

TrimEnd. The String. TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed.