Generic Instance Method


Replaces the text within the specified bounds with the given characters.


mutating func replaceSubrange<C>(_ bounds: Range<String.Index>, with newElements: C) where C : Collection, C.Element == Character



The range of text to replace. The bounds of the range must be valid indices of the string.


The new characters to add to the string.


Calling this method invalidates any existing indices for use with this string.

Complexity: O(m), where m is the combined length of the string and newElements. If the call to replaceSubrange(_:with:) simply removes text at the end of the string, the complexity is O(n), where n is equal to bounds.count.


Generic Constraints

  • C : Collection
  • C.Element == Character

See Also

Replacing Substrings

func replaceSubrange<C, R>(R, with: C)

Replaces the specified subrange of elements with the given collection.

func replacingCharacters(in: Range<String.Index>, with: String)

Returns a new string in which the characters in a specified range of the String are replaced by a given string.

func replacingOccurrences(of: String, with: String, options: String.CompareOptions, range: Range<String.Index>?)

Returns a new string in which all occurrences of a target string in a specified range of the String are replaced by another given string.