- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift String removeAll() Function
The String structure provides various methods to remove either specified characters or the whole characters from the given string and the removeAll() method is one of them. For example, we have a string My Car, now we use the removeAll() method to remove all the characters. So the final answer is .
In Swift, we can use removeAll() method in two different ways-
- removeAll()
- removeAll(where:)
removeAll() Method
The removeAll() method is used to remove all the characters from the given string. Or we can say that it removes all the methods and returns an empty string.
Syntax
Following is the syntax of the removeAll() method −
func removeAll()
Parameters
This method does not take any parameters.
Return Value
This method returns an empty string.
Example 1
Swift program to demonstrate how to use the removeAll() method −
import Foundation
// Declaring a string
var str = "Swift"
print("Original String:", str)
// Removing all the characters from the string
// Using removeAll() function
print("Resultant String:", str.removeAll())
Output
Original String: Swift Resultant String: ()
Example 2
Swift program to remove all the characters from the given string −
import Foundation
// Declaring a string
var str = "Hello Mohit"
// Removing all the characters from the string
// Using removeAll() function
str.removeAll()
// Checking if the string is empty or not
// after removing all the characters
if (str.isEmpty){
print("The given string is empty")
}
else{
print("The given string is not empty")
}
Output
The given string is empty
removeAll(where:) Method
The removeAll(where:) method is used to remove all the characters from the given string that match the specified condition.
Syntax
Following is the syntax of the removeAll(where: ) method −
func removeAll(where myClosure: condition)
Parameters
This method has a closure that takes each character from the given string as an argument and returns a boolean value that indicates whether the character should be removed from the given string or not.
Return Value
This method returns a string.
Example 1
Swift program to demonstrate how to use the removeAll (where:) method −
import Foundation
// Declaring a string
var str = "Hello Mohit"
print("Original string", str)
// Characters we want to remove from the string
let reChars: Set<Character> = ["e", "o", "l"]
// Removing only those characters that satisfy the given condition
// Using removeAll(where:) method
str.removeAll(where: { reChars.contains($0) })
print("Updated string: ", str)
Output
Original string Hello Mohit Updated string: H Mhit
Example 2
Swift program to remove all the appearance of the given character from the string that satisfies the given condition −
import Foundation
// Declaring a string
var str = "Hello Mohit"
print("Original string", str)
// Removing all the appearance of "l" character
// Using removeAll(where:) method
str.removeAll(where: {$0 == "l"})
print("Updated string: ", str)
Output
Original string Hello Mohit Updated string: Heo Mohit