The standard library’s String type provides many useful string-related methods. Here are some examples to give you a sense of the available functionality.
When you run this program, you’ll see:
In Swift, many of these operations are methods on the String type itself, rather than functions in a separate package. This makes the syntax slightly different from the original example, but the functionality is very similar.
Note that Swift’s String is Unicode-correct and works with extended grapheme clusters, which can make some operations more complex but also more accurate for international text. The Index operation, for example, is more complicated in Swift due to this Unicode-correctness.
The Replace operation with a limit is not directly available in Swift, so we’ve simulated it by using a range for the second example.