Swift Strings - Swift Programming

What is Swift Strings?

Strings is considered as a sequence of characters. In Swift, they are an ordered collection of characters, like "Hello, World!" and these strings are represented by the Swift data type String, which in turn represents a collection of values of Character type.

Create a String

We can create a String either by using a string literal or creating an instance of a String class as stated below −

On executing the above code, it produces the below output −

Empty String

We can create an empty String by using an empty string literal or by creating an instance of String class as displayed below. We can also check whether a string is empty or not using the Boolean property isEmpty.

On executing the above code, it produces the below result −

String Constants

We can define whether our String will be altered (or mutated) by allocating it to a variable, or it will be constant by assigning it to a constant using let keyword as stated below −

variables, it produces the below output −

String Interpolation

String interpolation is just a simple way to create a new String value with a mix of constants, literals,variables and expressions by including their values inside a string literal.

Every item (variable or constant) that we will be inserting into the string literal is wrapped in a pair of parentheses,which is prefixed by a backslash. Below is a simple example −

On running the above code, it produces the below output −

String Concatenation

We can use the Plus( +) operator in order to concatenate two strings or a string and a character, or two characters. Below is a simple example −

On running the above code, it produces the below result −

String Length

Swift strings do not have a length property, but we can use the global count() function in order to count the number of characters in a string. Below is a simple example −

On running the above code,, it produces the below result −

String Comparison

We can use the == operator in order to compare two strings variables or constants. Here is a simple example −

On running the above code,, it produces the below output−

Unicode Strings

We can access a UTF-8 and UTF-16 representation of a String just by iterating over its utf8 and utf16 properties as explained in the below code −

On running the above code, it produces the below output −

String Functions & Operators

Swift maintains a wide range of methods and operators related to Strings −

S.No

Functions/Operators & Purpose

1

isEmpty

A Boolean value that determines whether a string is empty or not.

2

hasPrefix(prefix: String)

Function to check whether a given parameter string exists as a prefix of the string or not.

3

hasSuffix(suffix: String)

Function to check whether a given parameter string exists as a prefix of the string or not.

4

toInt()

Function to convert numeric String value into Integer.

5

count()

Global function to count the number of Characters in a string.

6

utf8

Property to return aUTF-8representation of a string.

7

utf16

Property to return aUTF-16representation of a string.

8

unicodeScalars

Property to return aUnicode Scalarrepresentation of a string.

9

+

Operator to concatenate two strings, or a string and a character, or two characters.

10

+=

Operator to append a string or character to an existing string.

11

==

Operator to determine the equality of two strings.

12

<

Operator to perform a lexicographical comparison to determine whether one string evaluates as less than another.

13

==

Operator to determine the equality of two strings.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Swift Programming Topics