What is the most succinct way to remove the first character from a string in Swift?


In Swift, we can use methods like dropFirst, Index(after:), and many others that can remove the first character of a string. In this article, we are going to learn various examples of these functions and understand how can we remove the first character of a string.

Using the dropFirst method

In this method, we are going to use the dropFirst method to remove the first character from the string and make the new string start with the second character. In case the original string is empty or has a single character in it, the result will be an empty string.

Example

import Foundation
let originalString = "This is a sample string to remove the first character."
let modifiedString = String(originalString.dropFirst())
print("Original string: \(originalString)")
print("Modified string: \(modifiedString)")

Output

Original string: This is a sample string to remove the first character.
Modified string: his is a sample string to remove the first character.

Using the suffix Method

In this method, we will create a substring that starts at the second character of the original string and includes all the remaining characters using the suffix method.

Example

import Foundation
let originalString = "This is a sample string to remove the first character."
let modifiedString = String(originalString.suffix(originalString.count - 1))
print("Original string: \(originalString)")
print("Modified string: \(modifiedString)")

Output

Original string: This is a sample string to remove the first character.
Modified string: his is a sample string to remove the first character.

Using the index(after:) Method

In this method, we will get the index of the second character in the string and creates a substring that starts at that index and includes all the remaining characters.

Example

import Foundation
let originalString = "This is a sample string to remove the first character."
let index = originalString.index(after: originalString.startIndex)
let modifiedString = String(originalString[index...])
print("Original string: \(originalString)")
print("Modified string: \(modifiedString)")

Output

Original string: This is a sample string to remove the first character.
Modified string: his is a sample string to remove the first character.

Using the removeFirst Method

In this method, the originalString is modified without the initial character. You can only use this method if you are certain that the string has at least two characters because calling removeFirst() will result in a runtime error if the original string is empty or only contains one character.

Example

import Foundation
var originalString = "This is a sample string to remove the first character."
print("Original string: \(originalString)")
originalString.removeFirst()
print("Modified string: \(originalString)")

Output

Original string: This is a sample string to remove the first character.
Modified string: his is a sample string to remove the first character.

Using the index(offsetBy:) Method

In this method, by executing index( :offsetBy:) with an offset of 1, which provides the index of the character one position after the initial index, this obtains the index of the second character in the string. The remaining characters are then included in a substring that is created starting at that index. Remember that this technique requires at least two characters in the string as a precondition. A runtime error will occur if you call index( :offsetBy:) with an offset of 1 if the string is empty or contains only one character.

Example

import Foundation
extension String {
   func chopPrefix(_ count: Int = 1) -> String {
      if count >= 0 && count <= self.count {
         let indexStartOfText = self.index(self.startIndex, offsetBy: count)
         return String(self[indexStartOfText...])
      }
      return ""
   }
}
let originalString = "This is a sample string to remove the first character."
let modifiedString = originalString.chopPrefix()
print("Original string: \(originalString)")
print("Modified string: \(modifiedString)")

Output

Original string: This is a sample string to remove the first character.
Modified string: his is a sample string to remove the first character.

Conclusion

In this article, we have understood the application of various swift methods like - dropFirst, removeFirst, Index, and Suffix. A new string is created where the first character of the original string is missing, as we all know that strings are immutable in swift.

Updated on: 11-Apr-2023

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements