Swift String – dropFirst()
dropFirst() in Swift is used to remove the first character from the string. By default, it returns the string by removing the first character. If you want a particular number of characters from the first, you need to specify an integer value as a parameter that represents the total number of characters to be deleted from the first in the swift string.
Syntax:
Parameter:
It takes only one optional parameter.
value – specifies the integer value to delete characters in the swift string from the first.
To understand this method much better, we have to see the examples in detail.
Example 1:
Let’s create a string and delete the first character from it without specifying any parameter using dropFirst().
var swift_string:String = "Potassium,Carbon"
print("Original string - ",swift_string)
//remove only the first character.
print("swift_string after deleting first character - ",swift_string.dropFirst())
Output:
Explanation:
Line 2:
We created a string named swift_string that holds – “Potassium,Carbon”.
Line 7:
Now, we are displaying the swift_string by deleting the first character.
The output is “otassium,Carbon”.
Example 2:
Let’s create a string and delete the first 5 characters from it by specifying the value as 5.
var swift_string:String = "Potassium,Carbon"
print("Original string - ",swift_string)
//remove first 5 characters
print("swift_string after deleting first 5 characters - ",swift_string.dropFirst(5))
Output:
Explanation:
Line 2:
We created a string named swift_string that holds – “Potassium,Carbon”.
Line 7:
Now, we are displaying the swift_string by deleting the first 5 characters.
The output is “sium,Carbon”. The characters that are removed are – “P,o,t,a,s”.
Suppose the string length is 4. If you specify value as 5, then the dropFirst() method won’t return any error. It will delete the first 4 characters.
Example 3:
Let’s create a string and delete the first 10 characters from it by specifying the value as 10.
var swift_string:String = "Nitrogen"
print("Original string - ",swift_string)
//remove first 10 characters
print("swift_string after deleting first 10 characters - ",swift_string.dropFirst(10))
Output:
Explanation:
Line 2:
We created a string named swift_string that holds – “Nitrogen”.
Line 7:
Now, we are displaying the swift_string by deleting the first 10 characters.
The output is empty. All the characters are removed.
Example 4:
Let’s create a string and delete the first 4 characters from it by specifying the value as 4. Now, compare it with the actual string inside the if statement.
var swift_string:String = "Nitrogen"
print("Original string - ",swift_string)
//check if both strings are equal or not.
if(swift_string==swift_string){
print("Both the strings are same")
}
else{
print("Both the strings are not same")
}
//remove first 4 characters and
//check if both strings are equal or not.
if(swift_string==swift_string.dropFirst(4)){
print("Both the strings are same")
}
else{
print("Both the strings are not same")
}
Output:
Explanation:
Line 2:
We created a string named swift_string that holds – “Nitrogen”.
Line 7-12:
First we are checking the same strings are same or not
From the output, they are the same.
Line 15-21:
Now, delete the first 4 characters from swift_string and compare them with the string: they are not the same.
Conclusion
In this swift tutorial, we saw how to delete the first characters from a swift string using the dropFirst() method. If you want a particular number of characters from the first, you need to specify an integer value as a parameter that represents the total number of characters to be deleted from the first in the swift string. We also discussed this method with if-else statements to understand this concept much better.
Source: linuxhint.com