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
swift_strings.htm
Advertisements