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

# addingPercentEscapes(using:)

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

```
func addingPercentEscapes(using enc: UInt) -> String?
```

## Parameters

`enc`

The encoding to use for the returned string. If you are uncertain of the correct encoding you should use [`NSUTF8StringEncoding`](/documentation/Foundation/NSUTF8StringEncoding).

## Return Value

A representation of the receiver using `encoding` to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns `nil` if `encoding` cannot encode a particular character.

## Discussion

It may be difficult to use this function to “clean up” unescaped or partially escaped URL strings where sequences are unpredictable. See <doc://com.apple.documentation/documentation/CoreFoundation/CFURLCreateStringByAddingPercentEscapes(_:_:_:_:_:)> for more information.

## See Also

[`-  stringByAddingPercentEncodingWithAllowedCharacters:`](/documentation/Foundation/NSString/addingPercentEncoding(withAllowedCharacters:))

Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.

[`stringByRemovingPercentEncoding`](/documentation/Foundation/NSString/removingPercentEncoding)

Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.



---

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)