Strings in Swift

⭐️ 👀 2023 WEB DEVELOPMENT BOOTCAMP starting in days! Join the waiting list to reserve your spot in my 10-weeks cohort course and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much much more! 👀 ⭐️

This tutorial belongs to the Swift series

Strings are one of the most popular tools in programming.

In Swift, a string can be defined using the string literal syntax:

let name = "Roger"

We use double quotes. Single quotes are not valid string delimiters.

A string can span over multiple lines, using 3 double quotes:

let description = """
	a long
  	long 
      	long description
	"""

You can use string interpolation to embed an expression in a string:

let age = 8

let name = """
	Roger, age \(age)
	Next year he will be \(age + 1)
	"""

Concatenate two strings with the + operator:

var name = "Roger"
name = name + " The Dog"

Append text to a string with the += operator:

var name = "Roger"
name += " The Dog"

Or using the append(_:) method:

var name = "Roger"
name.append(" The Dog")

You can count the characters in a string using the count string property:

let name = "Roger"
name.count //5

Any string comes with a set of useful methods, for example:

  • removeFirst() to remove the first character
  • removeLast() to remove the last character
  • lowercased() to get a new string, lowercased
  • uppercased() to get a new string, uppercased
  • starts(with:) which returns true if the string starts with a specific substring
  • contains() which returns true if the string contains a specific character

and many, many more.

When you need to reference an item into the string, since strings in Swift are unicode, we can’t simply reference the letter o in let name = "Roger" using name[1]. You need to work with indexes.

Any string provides the starting index with the startIndex property:

let name = "Roger"
name.startIndex //0

To calculate a specific index in the string, you calculate it using the index(i:offsetBy:) method:

let name = "Roger"
let i = name.index(name.startIndex, offsetBy: 2)
name[i] //"g"

The index can be used also to get a substring:

let name = "Roger"
let i = name.index(name.startIndex, offsetBy: 2)
name.suffix(from: i) //"ger"

//Or using the subscript:

name[i...] //"ger"

When you get a substring from a string, the type of the result is Substring, not String.

let name = "Roger"
let i = name.index(name.startIndex, offsetBy: 2)
print(type(of: name.suffix(from: i))) 
//Substring

Substrings are more memory efficient, because you do not get a new string, but the same memory structure is used behind the scenes, although you need to be careful when you deal with strings a lot, as there are optimizations you can implement.

Strings are collections, and they can be iterated over in loops.

One more thing! ⚠️ ✋

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more! 

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit bootcamp.dev