Swift Strings
Swift strings are a collection of characters. For example, an ordered collection of values of the Character type like "Hello, World!" has a data type of **String**.\\n\\n* * *\\n\\n## Creating a String\\n\\nYou can create a string using a string literal or an instance of the String class:\\n\\nimport Cocoa// use string literal var stringA = "Hello, World!"print( stringA )// String instantiate var stringB = String("Hello, World!")print( stringB )\\nThe output of the above program execution is:\\n\\nHello, World!Hello, World!\\n\\n* * *\\n\\n## Empty String\\n\\nYou can create an empty string by assigning an empty string literal to a variable or initializing an instance of the String class. We can use the string property isEmpty to check whether a string is empty:\\n\\nimport Cocoa// use string literal to create empty string var stringA = ""if stringA.isEmpty { print( "stringA is empty" )} else { print( "stringA is not empty" )}// instantiate String class to create empty string let stringB = String()if stringB.isEmpty { print( "stringB is empty" )} else { print( "stringB is not empty" )}\\nThe output of the above program execution is:\\n\\nstringA is empty stringB is empty\\n\\n* * *\\n\\n## String Constants\\n\\nYou can assign a string to a variable or a constant. Variables are mutable, while constants are immutable.\\n\\nimport Cocoa// stringA can be modified var stringA = "οΌ" stringA += ""print( stringA )// stringB cannot modify let stringB = String("οΌ") stringB += ""print( stringB )\\nThe above program execution will result in an error because stringB is a constant and cannot be modified:\\n\\nerror: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant stringB += ""\\n\\n* * *\\n\\n## String Interpolation\\n\\nString interpolation is a way to construct a new string, which can include constants, variables, literals, and expressions. Each item you insert into the string literal is enclosed in parentheses prefixed with a backslash:\\n\\nimport Cocoavar varA = 20let constA = 100var varC:Float = 20.0var stringA = "(varA) multiplied by (constA) equals (varC * 100)"print( stringA )\\nThe output of the above program execution is:\\n\\n20 multiplied by 100 equals 2000.0\\n\\n* * *\\n\\n## String Concatenation\\n\\nStrings can be concatenated using the **+** operator, as shown in the following example:\\n\\nimport Cocoalet constA = "οΌ"let constB = ""var stringA = constA + constB print( stringA )\\nThe output of the above program execution is:\\n\\nοΌ\\n\\n* * *\\n\\n## String Length\\n\\nString length is calculated using the **String.count** property, as shown in the following example:\\n\\n**Swift 3 version uses String.characters.count**\\n\\nimport Cocoavar varA = "example.com"print( "(varA), length is (varA.count)" )\\nThe output of the above program execution is:\\n\\nexample.com, length is 14\\n\\n* * *\\n\\n## String Comparison\\n\\nYou can use **==** to compare whether two strings are equal:\\n\\nimport Cocoavar varA = "Hello, Swift!"var varB = "Hello, World!"if varA == varB { print( "(varA) and (varB) is equal" )} else { print( "(varA) and (varB) is not equal" )}\\nThe output of the above program execution is:\\n\\nHello, Swift! and Hello, World! is not equal\\n\\n* * *\\n\\n## Unicode Strings\\n\\nUnicode is an international standard for text encoding, and Swift's String type is built upon Unicode. You can iterate over the UTF-8 and UTF-8 encodings within a string, as shown in the following example:\\n\\nimport Cocoavar unicodeString = ""print("UTF-8 Encoding: ")for code in unicodeString.utf8 { print("(code) ")}print("n")print("UTF-16 Encoding: ")for code in unicodeString.utf16 { print("(code) ")}\\nThe output of the above program execution is:\\n\\nUTF-8 Encoding: 232 143 156 233 184 159 230 149 153 231 168 139 UTF-16 Encoding: 33756 40479 25945 31243 \\n\\n* * *\\n\\n## String Functions and Operators\\n\\nSwift supports the following string functions and operators:\\n\\n| Index | functions/Operator & Description |\\n| --- | --- |\\n| 1 | **isEmpty** Checks whether a string is empty, returns a Boolean value |\\n| 2 | **hasPrefix(prefix: String)** Checks whether a string has a specific prefix |\\n| 3 | **hasSuffix(suffix: String)** Checks whether a string has a specific suffix. |\\n| 4 | **Int(String)** Converts a numeric string to an integer. Example: let myString: String = "256"let myInt: Int? = Int(myString) |\\n| 5 | **String.count** **Swift 3 version uses String.characters.count** Calculates the length of the string |\\n| 6 | **utf8** You can access its UTF-8 encoding by iterating over the utf8 property of the String |\\n| 7 | **utf16** You can access its UTF-16 encoding by iterating over the utf16 property of the String |\\n| 8 | **unicodeScalars** You can access its Unicode scalar encoding by iterating over the unicodeScalars property of the String value. |\\n| 9 | **+** Concatenates two strings and returns a new string |\\n| 10 | **+=** Concatenates the strings on both sides of the operator and assigns the new string to the variable on the left side of the operator |\\n| 11 | **==** Checks whether two strings are equal |\\n| 12 | **<** Compares two strings by comparing their letters one by one. |\\n| 13 | **!=** Checks whether two strings are not equal. |
YouTip