---
title: String.UTF8View
framework: swift
role: symbol
role_heading: Structure
path: swift/string/utf8view
---

# String.UTF8View

A view of a string’s contents as a collection of UTF-8 code units.

## Declaration

```swift
@frozen struct UTF8View
```

## Overview

Overview You can access a string’s view of UTF-8 code units by using its utf8 property. A string’s UTF-8 view encodes the string’s Unicode scalar values as 8-bit integers. let flowers = "Flowers 💐" for v in flowers.utf8 {     print(v) } // 70 // 108 // 111 // 119 // 101 // 114 // 115 // 32 // 240 // 159 // 146 // 144 A string’s Unicode scalar values can be up to 21 bits in length. To represent those scalar values using 8-bit integers, more than one UTF-8 code unit is often required. let flowermoji = "💐" for v in flowermoji.unicodeScalars {     print(v, v.value) } // 💐 128144

for v in flowermoji.utf8 {     print(v) } // 240 // 159 // 146 // 144 In the encoded representation of a Unicode scalar value, each UTF-8 code unit after the first is called a continuation byte. UTF8View Elements Match Encoded C Strings Swift streamlines interoperation with C string APIs by letting you pass a String instance to a function as an Int8 or UInt8 pointer. When you call a C function using a String, Swift automatically creates a buffer of UTF-8 code units and passes a pointer to that buffer. The code units of that buffer match the code units in the string’s utf8 view. The following example uses the C strncmp function to compare the beginning of two Swift strings. The strncmp function takes two const char* pointers and an integer specifying the number of characters to compare. Because the strings are identical up to the 14th character, comparing only those characters results in a return value of 0. let s1 = "They call me 'Bell'" let s2 = "They call me 'Stacey'"

print(strncmp(s1, s2, 14)) // Prints "0" print(String(s1.utf8.prefix(14))!) // Prints "They call me '" Extending the compared character count to 15 includes the differing characters, so a nonzero result is returned. print(strncmp(s1, s2, 15)) // Prints "-17" print(String(s1.utf8.prefix(15))!) // Prints "They call me 'B"

## Topics

### Instance Properties

- [customPlaygroundQuickLook](swift/string/utf8view/customplaygroundquicklook.md)
- [span](swift/string/utf8view/span.md)

### Instance Methods

- [isTriviallyIdentical(to:)](swift/string/utf8view/istriviallyidentical(to:).md)

### Default Implementations

- [BidirectionalCollection Implementations](swift/string/utf8view/bidirectionalcollection-implementations.md)
- [Collection Implementations](swift/string/utf8view/collection-implementations.md)
- [CustomDebugStringConvertible Implementations](swift/string/utf8view/customdebugstringconvertible-implementations.md)
- [CustomReflectable Implementations](swift/string/utf8view/customreflectable-implementations.md)
- [CustomStringConvertible Implementations](swift/string/utf8view/customstringconvertible-implementations.md)
- [Sequence Implementations](swift/string/utf8view/sequence-implementations.md)

## Relationships

### Conforms To

- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [Collection](swift/collection.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomReflectable](swift/customreflectable.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Escapable](swift/escapable.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [Sequence](swift/sequence.md)

## See Also

### Related String Types

- [Substring](swift/substring.md)
- [StringProtocol](swift/stringprotocol.md)
- [String.Index](swift/string/index.md)
- [String.UnicodeScalarView](swift/string/unicodescalarview.md)
- [String.UTF16View](swift/string/utf16view.md)
- [String.Iterator](swift/string/iterator.md)
- [String.Encoding](swift/string/encoding.md)
