Tutorial by Examples

String literals in Swift are delimited with double quotes ("): let greeting = "Hello!" // greeting's type is String Characters can be initialized from string literals, as long as the literal contains only one grapheme cluster: let chr: Character = "H" // valid let chr...
Concatenate strings with the + operator to produce a new string: let name = "John" let surname = "Appleseed" let fullName = name + " " + surname // fullName is "John Appleseed" Append to a mutable string using the += compound assignment operator, or usi...
Check whether a string is empty: if str.isEmpty { // do something if the string is empty } // If the string is empty, replace it with a fallback: let result = str.isEmpty ? "fallback string" : str Check whether two strings are equal (in the sense of Unicode canonical equivale...
A Swift String is made of Unicode code points. It can be decomposed and encoded in several different ways. let str = "ที่👌①!" Decomposing Strings A string's characters are Unicode extended grapheme clusters: Array(str.characters) // ["ที่", "👌", "①", ...
Setting values Using Unicode directly var str: String = "I want to visit 北京, Москва, मुंबई, القاهرة, and 서울시. 😊" var character: Character = "🌍" Using hexadecimal values var str: String = "\u{61}\u{5927}\u{1F34E}\u{3C0}" // a大🍎π var character: Character = &quo...
2.2 let aString = "This is a test string." // first, reverse the String's characters let reversedCharacters = aString.characters.reverse() // then convert back to a String with the String() initializer let reversedString = String(reversedCharacters) print(reversedString) // &q...
To make all the characters in a String uppercase or lowercase: 2.2 let text = "AaBbCc" let uppercase = text.uppercaseString // "AABBCC" let lowercase = text.lowercaseString // "aabbcc" 3.0 let text = "AaBbCc" let uppercase = text.uppercased() // &qu...
Letters 3.0 let letters = CharacterSet.letters let phrase = "Test case" let range = phrase.rangeOfCharacter(from: letters) // range will be nil if no letters is found if let test = range { print("letters found") } else { print("letters not found") }...
Given a String and a Character let text = "Hello World" let char: Character = "o" We can count the number of times the Character appears into the String using let sensitiveCount = text.characters.filter { $0 == char }.count // case-sensitive let insensitiveCount = text.low...
2.2 func removeCharactersNotInSetFromText(text: String, set: Set<Character>) -> String { return String(text.characters.filter { set.contains( $0) }) } let text = "Swift 3.0 Come Out" var chars = Set([Character]("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ&...
Leading Zeros let number: Int = 7 let str1 = String(format: "%03d", number) // 007 let str2 = String(format: "%05d", number) // 00007 Numbers after Decimal let number: Float = 3.14159 let str1 = String(format: "%.2f", number) // 3.14 let str2 = String(format: &...
Int("123") // Returns 123 of Int type Int("abcd") // Returns nil Int("10") // Returns 10 of Int type Int("10", radix: 2) // Returns 2 of Int type Double("1.5") // Returns 1.5 of Double type Double("abcd") // Returns nil Note that do...
3.0 let string = "My fantastic string" var index = string.startIndex while index != string.endIndex { print(string[index]) index = index.successor() } Note: endIndex is after the end of the string (i.e. string[string.endIndex] is an error, but string[string.startIndex] i...
3.0 let someString = " Swift Language \n" let trimmedString = someString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) // "Swift Language" Method stringByTrimmingCharactersInSet returns a new string made by removing from both ends of t...
To convert String to and from Data / NSData we need to encode this string with a specific encoding. The most famous one is UTF-8 which is an 8-bit representation of Unicode characters, suitable for transmission or storage by ASCII-based systems. Here is a list of all available String Encodings Stri...
In Swift you can easily separate a String into an array by slicing it at a certain character: 3.0 let startDate = "23:51" let startDateAsArray = startDate.components(separatedBy: ":") // ["23", "51"]` 2.2 let startDate = "23:51" let sta...

Page 1 of 1