Swift String removeAll()

The removeAll() method removes all the elements from the string based on a given condition.

Example


removeAll() Syntax

The syntax of removeAll() is:

string.removeAll(where: condition)

Here, string is an object of the String class.


removeAll() Parameters

The removeAll() method takes a single parameter:

  • condition - a closure which accepts a condition and returns a bool value. If the condition is true, the specified characters are removed from string.

removeAll() Return Value

The removeAll() method doesn't return any value. It only removes characters from the string.


Example: Swift removeAll()

In the above example, we have created a set named remove with characters that are to be removed from the string text.

We have defined the closure {remove.contains($0)} to remove the repeated characters.

$0 is the shortcut to mean that the first element of the remove set is passed into the closure.