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:
Hello world! This is JavaTpoint This is an example of multiple line string by JavaTpoint
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.
stringA is empty stringB is empty
Operations on Strings
We can operate a lot of operations on Swift strings.
The + operator is used to concatenate two strings or a string and a character, or two characters in Swift.
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.
10 times 1000 is equal to 10000.0
String 4 doesn't support length property, but we can use a global count() function to count the number of characters in a string.
Hello JavaTpoint, string length is 16
The == operator is used to compare two strings variables or constants.
Hello, JavaTpoint and Hello, World! are not equal
In Swift 4, strings are the collection of values, so we can iterate over strings using loops:
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.
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: