<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UTF8Span/CharacterIterator",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s8UTF8SpanV17CharacterIteratorV"
  },
  "title" : "UTF8Span.CharacterIterator"
}
-->

# UTF8Span.CharacterIterator

Iterate the `Character` contents of a `UTF8Span`.

```
struct CharacterIterator
```

## Overview

```
func countCharacters(_ string: borrowing String) {
    var iterator = string.utf8Span.makeCharacterIterator()
    var count = 0
    while let character = iterator.next() {
        count += 1
        print("Character \(count): \(character)")
    }
    print("Total: \(count) characters")
}

let string = "لاهور"
countCharacters(string)
// Prints "Character 1: ل"
// Prints "Character 2: ا"
// Prints "Character 3: ه"
// Prints "Character 4: و"
// Prints "Character 5: ر"
// Prints "Total: 5 characters"
```

## Topics

### Initializers

[`init(UTF8Span)`](/documentation/Swift/UTF8Span/CharacterIterator/init(_:))

### Instance Properties

[`let codeUnits: UTF8Span`](/documentation/Swift/UTF8Span/CharacterIterator/codeUnits)

[`var currentCodeUnitOffset: Int`](/documentation/Swift/UTF8Span/CharacterIterator/currentCodeUnitOffset)

The byte offset of the start of the next `Character`. This is always
scalar-aligned. It is always `Character`-aligned relative to the last
call to `reset` (or the start of the span if not called).

### Instance Methods

[`func next() -> Character?`](/documentation/Swift/UTF8Span/CharacterIterator/next())

Return the `Character` starting at `currentCodeUnitOffset`. After the
function returns, `currentCodeUnitOffset` holds the position at the
end of the `Character`, which is also the start of the next
`Character`.

[`func prefix() -> UTF8Span`](/documentation/Swift/UTF8Span/CharacterIterator/prefix())

Returns the UTF8Span containing all the content up to the iterator’s
current position.

[`func previous() -> Character?`](/documentation/Swift/UTF8Span/CharacterIterator/previous())

Return the `Character` ending at `currentCodeUnitOffset`. After the
function returns, `currentCodeUnitOffset` holds the position at the
start of the returned `Character`, which is also the end of the
previous `Character`.

[`func reset(roundingBackwardsFrom: Int)`](/documentation/Swift/UTF8Span/CharacterIterator/reset(roundingBackwardsFrom:))

Reset to the nearest character-aligned position `<= i`.

[`func reset(roundingForwardsFrom: Int)`](/documentation/Swift/UTF8Span/CharacterIterator/reset(roundingForwardsFrom:))

Reset to the nearest character-aligned position `>= i`.

[`func reset(toUnchecked: Int)`](/documentation/Swift/UTF8Span/CharacterIterator/reset(toUnchecked:))

Reset this iterator to `codeUnitOffset`, skipping *all* safety
checks.

[`func skipBack() -> Int`](/documentation/Swift/UTF8Span/CharacterIterator/skipBack())

Move `currentCodeUnitOffset` to the start of the previous `Character`,
without constructing it.

[`func skipBack(by: Int) -> Int`](/documentation/Swift/UTF8Span/CharacterIterator/skipBack(by:))

Move `currentCodeUnitOffset` to the start of the previous `n` `Character`s,
without constructing them.

[`func skipForward() -> Int`](/documentation/Swift/UTF8Span/CharacterIterator/skipForward())

Advance `currentCodeUnitOffset` to the end of the current `Character`,
without constructing it.

[`func skipForward(by: Int) -> Int`](/documentation/Swift/UTF8Span/CharacterIterator/skipForward(by:))

Advance `currentCodeUnitOffset` to the end of `n` `Characters`, without
constructing them.

[`func suffix() -> UTF8Span`](/documentation/Swift/UTF8Span/CharacterIterator/suffix())

Returns the UTF8Span containing all the content after the iterator’s
current position.

---

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)