Remove the last character from a string in the Swift language


There are several ways to remove the last character from a string. Let's see some examples of different methods.

We will use the below approaches to remove the character from a string −

  • Using the dropLast() method

  • Using the substring(to:) method

  • Using the removeLast() method

  • Using the prefix(upTo:) method

  • Using an array and removeLast() method

  • Using the remove(at:) method

Using the dropLast() method

In Swift, you can remove the last character from a string by using the String method dropLast(). Here is an example −

Example 

import Foundation
let originalString = "Tutorials Point!"
let modifiedString = originalString.dropLast()
print("Original String: \(originalString)")
print("Modified String: \(modifiedString)")

Output

Original String: Tutorials Point!
Modified String: Tutorials Point

This would remove the exclamation mark from the end of the string and store the resulting string ("Tutorials Point") in the variable modifiedString.

Using the substring(to:) method

Substring(to:) is another string method to remove the last character from the string and pass in the index of the character before the one you want to remove.

Example 

We can see the demonstration of the same in the following example −

import Foundation
let originalString = "Tutorials Points!"
let modifiedString = originalString.substring(to: originalString.index(before: originalString.endIndex))
print("Original String: \(originalString)")
print("Modified String: \(modifiedString)")

Output

Original String: Tutorials Point!
Modified String: Tutorials Point

This would also remove the exclamation mark from the end of the string and store the resulting string ("Tutorials Point") in the variable modifiedString.

Using the removeLast() method

Another way to do this is by using the removeLast() method of the RangeReplaceableCollection protocol which is adopted by the String class. Following the demonstration of the same

Example 

import Foundation
var originalString = "Tutorials Points!"
print("Original string before remove: \(originalString)")
originalString.removeLast()
print("Original string after remove: \(originalString)")

Output

Original string before remove: Tutorials Points!
Original string after remove: Tutorials Points

This method will remove the last character of the string and stores the modified string in same variable.

Using the prefix(upTo:) method

Another way to remove the last character from a string in Swift is by using the prefix(upTo:) method and passing in the index of the last character. Following is the demonstration of the same.

Example 

import Foundation
let originalString = "Tutorials Points!"
let modifiedString = String(originalString.prefix(upTo: originalString.index(before: originalString.endIndex)))
print("Original String: \(originalString)")
print("Modified String: \(modifiedString)")

This example will also remove the last character from the input string. You are passing the index value in prefix(upTo:) method.

Output

Original String: Tutorials Points!
Modified String: Tutorials Points

Using an array and removeLast() method

RemoveLast() is another method that removes the last character by converting the string to an array of characters . Following is a demonstration of the same.

Example 

import Foundation
let originalString = "Tutorials Points!"
var characters = Array(originalString)
characters.removeLast()
let modifiedString = String(characters)
print("Original String: \(originalString)")
print("Modified String: \(modifiedString)")

Output

Original String: Tutorials Points!
Modified String: Tutorials Points

This would also remove the last character from the string.

Using the remove(at:) method

String.remove(at:) method can also be used to remove the last character of the string. Following is a demonstration of the same.

Example 

import Foundation
var originalString = "Tutorials Points!"
print("Original string before remove: \(originalString)")
originalString.remove(at: originalString.index(before: originalString.endIndex))
print("Original string after remove: \(originalString)")

Output

Original string before remove: Tutorials Points!
Original string after remove: Tutorials Points

This will remove the last character of the string and the modified string will be stored in the same variable.

Conclusion

There are several methods to remove the last characters from a string in Swift programming language. There are some methods like dropLast(), substring(to:), removeLast(), prefix() you can use. In some of the methods, you can convert a string into array of characters and then remove last character from array using removeLast() function.

Updated on: 28-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements