How to remove a specific character from a string in Swift?
Last Updated :
05 Jul, 2022
Swift language supports different types of generic collections and a string is one of them. A string is a collection of alphabets. In the Swift string, we check the removal of a character from the string. To do this task we use the remove() function. This function is used to remove a character from the string. It will return a character that was removed from the given string.
Syntax:
string.remove(at: i)
Here string is an object of the string class.
Parameters: This function accepts a parameter that is illustrated below:
- i: It is a specified index of the character to remove.
Return Value: This function returns a character that was removed from the specified string.
Example 1:
Swift
import Swift
var string = "GFG"
var i = string.index(string.startIndex, offsetBy: 1)
var removed_String = string.remove(at: i)
print (string)
print (removed_String)
|
Output:
GG
F
Example 2:
Swift
import Swift
var string = "Geeks"
print ( "Before Removing:" , string)
var i = string.index(string.startIndex, offsetBy: 4)
var removed_String = string.remove(at: i)
print ( "After Removing:" , string)
print ( "Removed Character:" , removed_String)
|
Output:
Before Removing: Geeks
After Removing: Geek
Removed Character: s
Share your thoughts in the comments
Please Login to comment...