---
title: Substring
framework: swift
role: symbol
role_heading: Structure
path: swift/substring
---

# Substring

A slice of a string.

## Declaration

```swift
@frozen struct Substring
```

## Overview

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.

## Topics

### Operators

- [~=(_:_:)](swift/substring/~=(_:_:).md)

### Initializers

- [init()](swift/substring/init().md)
- [init(_:)](swift/substring/init(_:)-4njms.md)
- [init(_:)](swift/substring/init(_:)-61zpv.md)
- [init(_:)](swift/substring/init(_:)-7k0au.md)

### Instance Properties

- [base](swift/substring/base.md)
- [characters](swift/substring/characters.md)
- [customPlaygroundQuickLook](swift/substring/customplaygroundquicklook.md)
- [isContiguousUTF8](swift/substring/iscontiguousutf8.md)
- [utf8Span](swift/substring/utf8span.md)

### Instance Methods

- [filter(_:)](swift/substring/filter(_:).md)
- [isTriviallyIdentical(to:)](swift/substring/istriviallyidentical(to:).md)
- [makeContiguousUTF8()](swift/substring/makecontiguousutf8().md)
- [replaceSubrange(_:with:)](swift/substring/replacesubrange(_:with:)-mfwu.md)
- [withMutableCharacters(_:)](swift/substring/withmutablecharacters(_:).md)
- [withUTF8(_:)](swift/substring/withutf8(_:).md)

### Type Aliases

- [Substring.CharacterView](swift/substring/characterview.md)
- [Substring.Output](swift/substring/output.md)

### Default Implementations

- [Attachable Implementations](swift/substring/attachable-implementations.md)
- [BidirectionalCollection Implementations](swift/substring/bidirectionalcollection-implementations.md)
- [Collection Implementations](swift/substring/collection-implementations.md)
- [Comparable Implementations](swift/substring/comparable-implementations.md)
- [CustomDebugStringConvertible Implementations](swift/substring/customdebugstringconvertible-implementations.md)
- [CustomReflectable Implementations](swift/substring/customreflectable-implementations.md)
- [CustomStringConvertible Implementations](swift/substring/customstringconvertible-implementations.md)
- [Equatable Implementations](swift/substring/equatable-implementations.md)
- [ExpressibleByExtendedGraphemeClusterLiteral Implementations](swift/substring/expressiblebyextendedgraphemeclusterliteral-implementations.md)
- [ExpressibleByStringInterpolation Implementations](swift/substring/expressiblebystringinterpolation-implementations.md)
- [ExpressibleByStringLiteral Implementations](swift/substring/expressiblebystringliteral-implementations.md)
- [ExpressibleByUnicodeScalarLiteral Implementations](swift/substring/expressiblebyunicodescalarliteral-implementations.md)
- [Hashable Implementations](swift/substring/hashable-implementations.md)
- [LosslessStringConvertible Implementations](swift/substring/losslessstringconvertible-implementations.md)
- [RangeReplaceableCollection Implementations](swift/substring/rangereplaceablecollection-implementations.md)
- [Sequence Implementations](swift/substring/sequence-implementations.md)
- [StringProtocol Implementations](swift/substring/stringprotocol-implementations.md)
- [TextOutputStream Implementations](swift/substring/textoutputstream-implementations.md)
- [TextOutputStreamable Implementations](swift/substring/textoutputstreamable-implementations.md)

## Relationships

### Conforms To

- [Attachable](testing/attachable.md)
- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [Collection](swift/collection.md)
- [Comparable](swift/comparable.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomReflectable](swift/customreflectable.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [CustomTestStringConvertible](testing/customteststringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [ExpressibleByExtendedGraphemeClusterLiteral](swift/expressiblebyextendedgraphemeclusterliteral.md)
- [ExpressibleByStringInterpolation](swift/expressiblebystringinterpolation.md)
- [ExpressibleByStringLiteral](swift/expressiblebystringliteral.md)
- [ExpressibleByUnicodeScalarLiteral](swift/expressiblebyunicodescalarliteral.md)
- [Hashable](swift/hashable.md)
- [LosslessStringConvertible](swift/losslessstringconvertible.md)
- [RangeReplaceableCollection](swift/rangereplaceablecollection.md)
- [RegexComponent](swift/regexcomponent.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [Sequence](swift/sequence.md)
- [StringProtocol](swift/stringprotocol.md)
- [TextOutputStream](swift/textoutputstream.md)
- [TextOutputStreamable](swift/textoutputstreamable.md)

## See Also

### Related String Types

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