Substring

SubString is for strings like what ArraySlice is for arrays: a view of a part of a string, where its startIndex and endIndex are indices into the original string. It conforms to the same protocols as String:

Substring

StringProtocol contains many of the common text operations, so when you write functions that take a string parameter you can often use StringProtocol instead to also accept substrings. When you do, you have to use generics, as shown here:

func foo<S: StringProtocol>(s: S) {
  // use 's' almost like a normal string.
}

Just as with ArraySlice, substrings keep a reference to the entire string, so when you are done processing substrings you ...

Get Beginning Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.