Easy Tutorial
❮ Swift Characters Swift Access Control ❯

Swift Strings

Swift strings are a collection of characters. For example, an ordered collection of character type values like "Hello, World!" has the data type String.


Creating Strings

You can create a string by using a string literal or by instantiating an instance of the String class:

import Cocoa

// Using a string literal
var stringA = "Hello, World!"
print(stringA)

// Instantiating the String class
var stringB = String("Hello, World!")
print(stringB)

The output of the above program is:

Hello, World!
Hello, World!

Empty Strings

You can initialize an empty string by assigning an empty string literal to a variable or by initializing an instance of the String class. You can use the isEmpty property to check if a string is empty:

import Cocoa

// Using a string literal to create an empty string
var stringA = ""

if stringA.isEmpty {
   print("stringA is empty")
} else {
   print("stringA is not empty")
}

// Instantiating the String class to create an empty string
let stringB = String()

if stringB.isEmpty {
   print("stringB is empty")
} else {
   print("stringB is not empty")
}

The output of the above program is:

stringA is empty
stringB is empty

String Constants

You can assign a string to a variable or a constant. Variables can be modified, while constants cannot.

import Cocoa

// stringA can be modified
var stringA = "tutorialpro.org: "
stringA += "http://www.tutorialpro.org"
print(stringA)

// stringB cannot be modified
let stringB = String("tutorialpro.org: ")
stringB += "http://www.tutorialpro.org"
print(stringB)

The above program will result in an error because stringB is a constant and cannot be modified:

error: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant
stringB += "http://www.tutorialpro.org"

String Interpolation

String interpolation is a way to construct a new string value that includes constants, variables, literals, and expressions. Each item you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash:

import Cocoa

var varA = 20
let constA = 100
var varC: Float = 20.0

var stringA = "\(varA) times \(constA) equals \(varC * 100)"
print(stringA)

The output of the above program is:

20 times 100 equals 2000.0

String Concatenation

Strings can be concatenated using the + operator, as shown in the following example:

import Cocoa

let constA = "tutorialpro.org: "
let constB = "http://www.tutorialpro.org"

var stringA = constA + constB

print(stringA)

The output of the above program is:

tutorialpro.org: http://www.tutorialpro.org

String Length

The length of a string can be calculated using the String.count property, as shown in the following example:

For Swift 3, use String.characters.count

import Cocoa

var varA = "www.tutorialpro.org"

print("\(varA), length is \(varA.count)")

The output of the above program is:

www.tutorialpro.org, length is 14

String Comparison

You can compare two strings for equality using the == operator:

import Cocoa

var varA = "Hello, Swift!"
var varB = "Hello, World!"

if varA == varB {
   print("Both strings are equal")
} else {
   print("Both strings are not equal")
}

The output of the above program is:

Both strings are not equal
if varA == varB {
   print("\(varA) and \(varB) are equal")
} else {
   print("\(varA) and \(varB) are not equal")
}

The above program execution output is:

Hello, Swift! and Hello, World! are not equal

Unicode Strings

Unicode is an international standard for encoding text, and Swift's String type is built on Unicode. You can iterate over the UTF-8 and UTF-16 encodings of a string, as shown in the following example:

import Cocoa

var unicodeString = "tutorialpro.org"

print("UTF-8 Encoding: ")
for code in unicodeString.utf8 {
   print("\(code) ")
}

print("\n")

print("UTF-16 Encoding: ")
for code in unicodeString.utf16 {
   print("\(code) ")
}

The above program execution output is:

UTF-8 Encoding: 
232 
143 
156 
233 
184 
159 
230 
149 
153 
231 
168 
139 
UTF-16 Encoding: 
33756 
40479 
25945 
31243

String Functions and Operators

Swift supports the following string functions and operators:

No. Function/Operator & Description
1 isEmpty - Checks if the string is empty and returns a boolean value
2 hasPrefix(prefix: String) - Checks if the string has a specific prefix
3 hasSuffix(suffix: String) - Checks if the string has a specific suffix
4 Int(String) - Converts a string number to an integer.<br>Example: let myString: String = "256"<br>let myInt: Int? = Int(myString)
5 String.count - In Swift 3, it was String.characters.count to calculate the length of the string
6 utf8 - You can access its UTF-8 encoding by iterating over the String's utf8 property
7 utf16 - You can access its UTF-16 encoding by iterating over the String's utf16 property
8 unicodeScalars - You can access its Unicode scalar encoding by iterating over the String's unicodeScalars property
9 + - Concatenates two strings and returns a new string
10 += - Concatenates the strings on both sides of the operator and assigns the new string to the variable on the left
11 == - Checks if two strings are equal
12 < - Compares two strings by comparing each letter of the strings
13 != - Checks if two strings are not equal
❮ Swift Characters Swift Access Control ❯