Introduction
A string is a set of characters and swift strings are represented by string type and represent a collection of values of character type. Swift string and character type provide a unicode compliant to work with text in the code. String concatenation is a simple as adding together two strings with the+ operator. String mutability is managed by choosing between a constant or a variable.
Initializing a string
We can create empty string value as a starting point for building a longer string ether assign an empty string to a variable or initialize a new string instance with initializer syntax:
- var emptyString = ""
- var anotherEmptyString = String ()
-
- //both strings are empty
Check if the string value is empty or not with the below code:
- if emptyString.isEmpty
- {
- println ("NO String value")
- }
String Mutability
This is to indicate the particular string can be modified by assigning it to a variable or constant which cannot be modified. It is also to indicate whether a string can be mutated is from string mutation between two classes.
- var variable = "horse"
- variableString += "and carriage"
-
- let constant = "highlander"
- constantString += "and another highlander"
Value Types
Swift string type is a value type. The string is copied when it is passed to a function or method during the creation of a new string value. If a new copy of the existing string value is created and the new copy is passed or assigned. The swift compiler optimizes the usage that actual copying takes place if necessary.
Swift string type represents a collection of character values in a specified order. Each character value represents a single Unicode character. Accessing the individual character values in a string by iterating over that string with a for-in loop. Create a stand-alone character constant or variable from a single character string literal by providing a character type annotation.
Concatenating String and characters
String and character values can be added together with additional operators to create a new string value and it cannot append a string or character variable because a character value mus contain a single character only.
- let string1 = "hello"
- let string2 = "there"
- let character1 : character = "!"
- let character2 : character = "?"
-
- let stringPlusCharacter = string1 + character1
- //equals hello!
- let stringPlusString = string1 + string2
- //equals hello there
- let characterPlusString = character1 + string1
- //equals !hello
- let characterPlusCharacter = character1 + character2
- //equals !?
String Interpolation
String interpolation is used to construct a new string value from a group of constants or variables or expressions by including their values inside a string literal. The string is wrapped in a pair of parenthesis and prefixed by a backslash. The below example the value of the multiplier is inserted into a string literal as a multiplier and the placeholder is replaced with the actual value of multiplier when string interpolation is evaluated to create an actual string. The value of the multiplier is a part of the large expression in a string.
- let multiplier = 3
- let message = "\(multiplier) times 2.5 is \(Double(multiplier)*2.5)"
String Comparing
Swift has three types if comparing strings, namely string equality, prefix equality, suffix equality. These three methods are used to compare the string values,
String Equality
Two string values are considered equal if they contain exactly the same characters in the same order.
- let quotation = "Swift equality"
- let check = "Swift equality"
- if quotation == check
- {
- println("Two strings are Equal")
- }
Prefix and Suffix Equality
To check whether a string has a particular string prefix or suffix call string hasPrefix and hasSuffix methods. Both methods perform a character by character comparision between the base string and prefix string. Use the hasPrefix method with romeoAndJuliet array to count the number of scenes is shown below:
- var act1SceneCount = 0
- for scene in romeoAndJuliet
- {
- if scene.hasPrefix("Act 1")
- {
- ++at1SceneCount
- }
- }
- println("There are \(act1SceneCount) scenes in Act 1")
Uppercase and Lowercase string
An upper case and lower case of string can be accessed with its upperstrinig and lowercaseString properties:
- let normal = "Help"
- let shouty = normal.uppercaseString
- /print HELP
- let whisper = normal.lowercaseString
- // print help