---
title: "filter(_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/string/unicodescalarview/filter(_:)-8zki"
---

# filter(_:)

Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.

## Declaration

```swift
consuming func filter<E>(_ isIncluded: (Self.Element) throws(E) -> Bool) throws(E) -> Self where E : Error
```

## Parameters

- `isIncluded`: A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned collection.

## Return Value

Return Value A collection of the elements that isIncluded allowed.

## Discussion

Discussion In this example, filter(_:) is used to include only names shorter than five characters. let cast = ["Vivien", "Marlon", "Kim", "Karl"] let shortNames = cast.filter { $0.count < 5 } print(shortNames) // Prints "["Kim", "Karl"]" note: O(n), where n is the length of the collection.
