Strings And Characters In Swift


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: 
  1. var emptyString = ""  
  2. var anotherEmptyString = String ()  
  4. //both strings are empty  
Check if the string value is empty or not with the below code:
  1. if emptyString.isEmpty  
  2. {  
  3.  println ("NO String value")  
  4. }  

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.
  1. var variable = "horse"  
  2. variableString += "and carriage"  
  4. let constant = "highlander"  
  5. 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.
  1. let string1 = "hello"  
  2. let string2 = "there"  
  3. let character1 : character = "!"  
  4. let character2 : character = "?"  
  6. let stringPlusCharacter = string1 + character1  
  7. //equals hello!  
  8. let stringPlusString = string1 + string2  
  9. //equals hello there   
  10. let characterPlusString = character1 + string1  
  11. //equals !hello  
  12. let characterPlusCharacter  = character1 + character2  
  13. //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.
  1. let multiplier = 3  
  2. 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.
  1. let quotation = "Swift equality"    
  2. let check = "Swift equality"    
  3. if quotation == check    
  4. {    
  5.  println("Two strings are Equal")    
  6. }    

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: 
  1. var act1SceneCount = 0    
  2. for scene in romeoAndJuliet    
  3. {    
  4.  if scene.hasPrefix("Act 1")    
  5.    {    
  6.     ++at1SceneCount    
  7.    }    
  8.  }    
  9. 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:
  1. let normal = "Help"  
  2. let shouty = normal.uppercaseString  
  3. /print HELP  
  4. let whisper = normal.lowercaseString  
  5. // print help