Home » Swift Strings

Swift Strings

Swift 4 strings are ordered collection of characters, such as “Hello, World!” and they are represented by the Swift 4 data type String, which in turn represents a collection of values of Character type.

How to create a string?

A string can be created by using a string literal or creating an instance of a String class.

See this example:

Output:

Hello world!  This is tutoraspire  This is an  example of multiple line  string by tutoraspire  

Empty Strings

To create empty string, you can use an empty string literal or create an instance of string class.

To check whether the string is empty or not, you can use the Boolean property isEmpty.

Example:

Output:

stringA is empty  stringB is empty  

Operations on Strings

We can operate a lot of operations on Swift strings.

String Concatenation

The + operator is used to concatenate two strings or a string and a character, or two characters in Swift.

Example:

Output:

Hello tutoraspire  

String Interpolation

String interpolation is used to construct a new string value by mixing the values of constants, variables, literals, and expressions and include them inside a string literal. The values of variables and constants which you insert into string literal is wrapped in a pair of parentheses, prefixed by a backslash.

Example:

Output:

10 times 1000 is equal to 10000.0  

String Length

String 4 doesn’t support length property, but we can use a global count() function to count the number of characters in a string.

Example:

Output:

Hello tutoraspire, string length is 16  

String Comparison

The == operator is used to compare two strings variables or constants.

Example:

Output:

Hello, tutoraspire and Hello, World! are not equal  

String Iteration

In Swift 4, strings are the collection of values, so we can iterate over strings using loops:

Example:

Output:

W e l c o m e t o J a v a T p o i n t   

Iteration of Unicode Strings

We can access the UTF-8 and UTF-16 representation of the Unicode strings over its utf8 and utf16 properties.

Example:

Output:

UTF-8 Codes:   74   97   118   97   84   112   111   105   110   116       UTF-16 Codes:   74   97   118   97   84   112   111   105   110   116   

Swift 4 String Functions and Operators

A list of functions and operators related to String in Swift 4:

Index Functions/Operators Usage
1) isEmpty It is used to check whether a string is empty or not. It specifies a Boolean value.
2) hasPrefix(prefix: String) It is a function to check whether a given parameter string exists as a prefix of the string or not.
3) hasSuffix(suffix: String) It is a function to check whether a given parameter string exists as a suffix of the string or not.
4) toInt() It is a function to convert numeric String value into Integer.
5) count() It is a global function to count the number of Characters in a string.
6) utf8 It specifies a property to return a UTF-8 representation of a string.
7) utf16 It specifies a property to return a UTF-16 representation of a string.
8) unicodeScalars It specifies a property to return a Unicode Scalar representation of a string.
9) + It is an operator to concatenate two strings, or a string and a character, or two characters.
10) += It is an operator to append a string or character to an existing string.
11) == It is an operator to determine the equality of two strings.
12) < It is an operator to perform a lexicographical comparison to determine whether one string evaluates as less than another.
13) startIndex It is used to get the value at starting index of string.
14) endIndex It is used to get the value at ending index of string.
15) Indices It is used to access the indices one by one. i.e. all the characters of string one by one.
16) insert(“Value”, at: position) It is used to insert a value at a position.
17) remove(at: position)
removeSubrange(range)
It is used to remove a value at a position, or to remove a range of values from string.
18) reversed() It is used to return the reverse of a string.

Next TopicSwift Functions

You may also like