String Manipulations in Swift 4
Tags: programming
Published: 2018-09-02 18:07:38

---
title: String Manipulations in Swift 4
published: true
tags: programming
canonical_url: 
---

First thing first, Swift 4’s API for String has been beefed up with many powerful capabilities. Strings and String manipulations are essentials for any or most solutions. Swift language offers some powerful way to do this. So let’s review some of the language capabilities.

To create a string, you can assign it to a property:

![](https://cdn-images-1.medium.com/max/1024/1*fkQlOsljrPWLI73IKA18nQ.png)<figcaption>Sample constant string</figcaption>

What if you want to repeat a string multiple times? You can use the repeating feature:

![](https://cdn-images-1.medium.com/max/1024/1*wxChOom0H5c5Zce5c7J94g.png)<figcaption>Say my name, say my name…</figcaption>

Sometimes you need to represent a multi-line string and this can be done easily with 3 opening quotes and 3 closing quotes. Each line in the multi-line string literal contains an end-of-line character \n.

![](https://cdn-images-1.medium.com/max/1024/1*LSJ_rWKpYWbqOzTSWkdg2w.png)<figcaption>A multi-line String literal representation</figcaption>

To parse through each character in a String, you can do the following:

![](https://cdn-images-1.medium.com/max/1024/1*Yh2YsbXBgjoYNJ-YkC8t7Q.png)<figcaption>Loop through each of the character in a string</figcaption>

There are several ways to get the string portion or content between the two paragraph tags.

![](https://cdn-images-1.medium.com/max/1024/1*nXIXK46_7E2wKY4dKcjHxw.png)<figcaption>Using indexes to extract parts of a string</figcaption>

Let me explain what’s going on. The first approach above makes assumption that the <p></p> tags exist. So we use a fixed offset from the start and end of the string to extract the middle portion. On the other hand, we can use the second approach to determine the index based on identifiable parts of the <p></p> tags. Because we may not find the matching index, we default to using the start and end index of the original string. Which way do you prefer? Is there a better way of doing the same? Feel free to share it in the comments below.

Sometimes we may need to identify the position of the character in the string. How do we do this?

![](https://cdn-images-1.medium.com/max/1024/1*y5Ba8iScocfk2AP1aqAg0g.png)<figcaption>Looping through each of the characters in the String by their respective index (or position)</figcaption>

Note that we are advancing the offset by the position number from the String’s start index each time to retrieve the next character. Sometimes, you might only care about the position for “x” and “o” so you can use comparisons to only print those out. See the following example:

![](https://cdn-images-1.medium.com/max/1024/1*UTmpZyCvyuPFoGnVVC0Auw.png)<figcaption>Looping through the multi-line String literal to extract the position of the characters “x” and “o”</figcaption>

### Where to go from here?

String manipulations in Swift have come a long way. Check out the Apple documentation for more: [Swift Standard Library Reference — String](https://developer.apple.com/reference/swift/string) and post some of your favorite String manipulations in the comments below.