<!--
{
  "availability" : [
    "iOS: 2.0.0 - 2.0.0",
    "iPadOS: 2.0.0 - 2.0.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/string(withCString:length:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(cm)stringWithCString:length:"
  },
  "title" : "string(withCString:length:)"
}
-->

# string(withCString:length:)

Returns a string containing the characters in a given C-string.

```
class func string(withCString bytes: UnsafePointer<CChar>, length: Int) -> Any?
```

## Discussion

`cString` must not be NULL. `cString` should contain characters in the default C-string encoding. This method converts `length` * `sizeof(char)` bytes from `cString` and doesn’t stop short at a `NULL` character.

## See Also



---

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)