<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "swift: -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/String/init(validatingUTF8:)-2m5lb",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SS14validatingUTF8SSSgSays4Int8VG_tcfc"
  },
  "title" : "init(validatingUTF8:)"
}
-->

# init(validatingUTF8:)

Creates a new string by copying and validating the null-terminated UTF-8
data referenced by the given array.

```
init?(validatingUTF8 cString: [CChar])
```

## Parameters

`cString`


An array containing a null-terminated sequence of UTF-8 code units.

## Discussion

This initializer does not try to repair ill-formed UTF-8 code unit
sequences. If any are found, the result of the initializer is `nil`.

> Note: This initializer is deprecated. Use the initializer
> `String.init?(validating: array, as: UTF8.self)` instead,
> remembering that “\0” is a valid character in Swift.

---

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)