---
title: cStringLength()
framework: foundation
role: symbol
role_heading: Instance Method
path: foundation/nsstring/cstringlength()
---

# cStringLength()

Returns the length in char-sized units of the receiver’s C-string representation in the default C-string encoding.

## Declaration

```swift
func cStringLength() -> Int
```

## Discussion

Discussion Raises if the receiver can’t be represented in the default C-string encoding without loss of information. You can also use canBeConverted(to:) to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString() to get a C-string representation with some loss of information, then check its length explicitly using the ANSI function strlen().

## See Also

### Related Documentation

- [utf8String](foundation/nsstring/utf8string.md)
- [maximumLengthOfBytes(using:)](foundation/nsstring/maximumlengthofbytes(using:).md)
- [lengthOfBytes(using:)](foundation/nsstring/lengthofbytes(using:).md)

### Deprecated

- [string(withCString:)](foundation/nsstring/string(withcstring:).md)
- [init(CString:)](foundation/nsstring/init(cstring:)-vkuo.md)
- [string(withCString:length:)](foundation/nsstring/string(withcstring:length:).md)
- [init(CString:length:)](foundation/nsstring/init(cstring:length:)-5ure3.md)
- [init(CStringNoCopy:length:freeWhenDone:)](foundation/nsstring/init(cstringnocopy:length:freewhendone:)-86dm2.md)
- [string(withContentsOfFile:)](foundation/nsstring/string(withcontentsoffile:).md)
- [init(contentsOfFile:)](foundation/nsstring/init(contentsoffile:).md)
- [string(withContentsOf:)](foundation/nsstring/string(withcontentsof:).md)
- [init(contentsOfURL:)](foundation/nsstring/init(contentsofurl:).md)
- [write(toFile:atomically:)](foundation/nsstring/write(tofile:atomically:).md)
- [write(to:atomically:)](foundation/nsstring/write(to:atomically:).md)
- [getCharacters(_:)](foundation/nsstring/getcharacters(_:).md)
- [cString()](foundation/nsstring/cstring().md)
- [lossyCString()](foundation/nsstring/lossycstring().md)
- [getCString(_:)](foundation/nsstring/getcstring(_:).md)
