<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/trimmingCharacters(in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)stringByTrimmingCharactersInSet:"
  },
  "title" : "trimmingCharacters(in:)"
}
-->

# trimmingCharacters(in:)

Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

```
func trimmingCharacters(in set: CharacterSet) -> String
```

## Parameters

`set`

A character set containing the characters to remove from the receiver. `set` must not be `nil`.

## Return Value

A new string made by removing from both ends of the receiver characters contained in `set`. If the receiver is composed entirely of characters from `set`, the empty string is returned.

## Discussion

Use [`whitespaces`](/documentation/Foundation/NSCharacterSet/whitespaces) or [`whitespacesAndNewlines`](/documentation/Foundation/NSCharacterSet/whitespacesAndNewlines) to remove whitespace around strings.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)