<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Substring",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Ss"
  },
  "title" : "Substring"
}
-->

# Substring

A slice of a string.

```
@frozen struct Substring
```

## Overview

When you create a slice of a string, a `Substring` instance is the result.
Operating on substrings is fast and efficient because a substring shares
its storage with the original string. The `Substring` type presents the
same interface as `String`, so you can avoid or defer any copying of the
string’s contents.

The following example creates a `greeting` string, and then finds the
substring of the first sentence:

```
let greeting = "Hi there! It's nice to meet you! 👋"
let endOfSentence = greeting.firstIndex(of: "!")!
let firstSentence = greeting[...endOfSentence]
// firstSentence == "Hi there!"
```

You can perform many string operations on a substring. Here, we find the
length of the first sentence and create an uppercase version.

```
print("'\(firstSentence)' is \(firstSentence.count) characters long.")
// Prints "'Hi there!' is 9 characters long."

let shoutingSentence = firstSentence.uppercased()
// shoutingSentence == "HI THERE!"
```

# Converting a Substring to a String

This example defines a `rawData` string with some unstructured data, and
then uses the string’s `prefix(while:)` method to create a substring of
the numeric prefix:

```
let rawInput = "126 a.b 22219 zzzzzz"
let numericPrefix = rawInput.prefix(while: { "0"..."9" ~= $0 })
// numericPrefix is the substring "126"
```

When you need to store a substring or pass it to a function that requires a
`String` instance, you can convert it to a `String` by using the
`String(_:)` initializer. Calling this initializer copies the contents of
the substring to a new string.

```
func parseAndAddOne(_ s: String) -> Int {
    return Int(s, radix: 10)! + 1
}
_ = parseAndAddOne(numericPrefix)
// error: cannot convert value...
let incrementedPrefix = parseAndAddOne(String(numericPrefix))
// incrementedPrefix == 127
```

Alternatively, you can convert the function that takes a `String` to one
that is generic over the `StringProtocol` protocol. The following code
declares a generic version of the `parseAndAddOne(_:)` function:

```
func genericParseAndAddOne<S: StringProtocol>(_ s: S) -> Int {
    return Int(s, radix: 10)! + 1
}
let genericallyIncremented = genericParseAndAddOne(numericPrefix)
// genericallyIncremented == 127
```

You can call this generic function with an instance of either `String` or
`Substring`.

> Important: Don’t store substrings longer than you need them to perform a
> specific operation. A substring holds a reference to the entire storage
> of the string it comes from, not just to the portion it presents, even
> when there is no other reference to the original string. Storing
> substrings may, therefore, prolong the lifetime of string data that is
> no longer otherwise accessible, which can appear to be memory leakage.

## Relationships

### Conforms To

[`Attachable`](/documentation/Testing/Attachable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`LosslessStringConvertible`](/documentation/Swift/LosslessStringConvertible)

[`ExpressibleByExtendedGraphemeClusterLiteral`](/documentation/Swift/ExpressibleByExtendedGraphemeClusterLiteral)

[`Copyable`](/documentation/Swift/Copyable)

[`Comparable`](/documentation/Swift/Comparable)

[`Escapable`](/documentation/Swift/Escapable)

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

[`CustomTestStringConvertible`](/documentation/Testing/CustomTestStringConvertible)

[`TextOutputStreamable`](/documentation/Swift/TextOutputStreamable)

[`Collection`](/documentation/Swift/Collection)

[`ExpressibleByStringLiteral`](/documentation/Swift/ExpressibleByStringLiteral)

[`Hashable`](/documentation/Swift/Hashable)

[`ExpressibleByStringInterpolation`](/documentation/Swift/ExpressibleByStringInterpolation)

[`Sequence`](/documentation/Swift/Sequence)

[`TextOutputStream`](/documentation/Swift/TextOutputStream)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`ExpressibleByUnicodeScalarLiteral`](/documentation/Swift/ExpressibleByUnicodeScalarLiteral)

[`StringProtocol`](/documentation/Swift/StringProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`RangeReplaceableCollection`](/documentation/Swift/RangeReplaceableCollection)

[`Sendable`](/documentation/Swift/Sendable)

[`RegexComponent`](/documentation/Swift/RegexComponent)

---

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)