The dropFirst() method removes the first character of the string.
Example
dropFirst() Syntax
The syntax of the string dropFirst() method is:
string.dropFirst(i: Int)
Here, string is an object of the String class.
dropFirst() Parameter
The dropFirst() method can take a single parameter:
- i (optional) - number of characters to be dropped from the beginning of the string
dropFirst() Return Value
- returns a substring after removing the specified number of characters from the beginning of the string.
Example 1: Swift String dropFirst()
Output
ello World Hello World
In the above example, since str1 starts with whitespace, str1.dropFirst() removes the whitespace from the beginning of str1.
Example 2: Drop Multiple Number of Characters
Output
World wift
Here,
str.dropFirst(6)- removes the first 6 characters from strstr1.dropFirst(7)- removes the first 7 characters from str1