Created
August 29, 2015 06:36
-
-
Save joehsieh/5dffe9a5e49421c8cde5 to your computer and use it in GitHub Desktop.
SwiftDemo for String and characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
// increment operator | |
var i = 1 | |
i++ | |
i | |
i = 1 | |
++i | |
i | |
// comparison of string | |
let a = NSObject() | |
let b = NSObject() | |
let c = a | |
a === b | |
a === c | |
// range operators | |
for index in 1...5 { | |
index | |
} | |
// Characters | |
for char in "Dog"{ | |
print(char) | |
} | |
let chars: [Character] = ["C", "a", "t"] | |
let charsString = String(chars) | |
// unicode scalars | |
var str = "\u{1F425}" | |
let message = "\"Hello World\"" | |
// extend grapheme clusters | |
let eAcute: Character = "\u{E9}" | |
let combinedEAcute: Character = "\u{65}\u{301}" | |
let precomposed: Character = "\u{D55C}" | |
let c1 = "\u{1112}" | |
let c2 = "\u{1161}" | |
let c3 = "\u{11AB}" | |
let decomposed = "\(c1)\(c2)\(c3)" | |
// counting characters | |
var word = "cafe" | |
count(word) | |
word += "\u{301}" | |
count(word) | |
// accessing string | |
let greeting = "Guten Tag" | |
let startIndex = greeting.startIndex | |
let endIndex = greeting.endIndex | |
greeting[startIndex] | |
greeting[startIndex.successor()] | |
greeting[endIndex.predecessor()] | |
let index = advance(greeting.startIndex, 7) | |
greeting[index] | |
for index in indices(greeting) { | |
greeting[index] | |
} | |
// inserting and removing | |
var welcome = "hello" | |
welcome.insert("!", atIndex: welcome.endIndex) | |
welcome.splice(" there", atIndex: welcome.endIndex.predecessor()) | |
welcome.removeAtIndex(welcome.endIndex.predecessor()) | |
welcome = "hello there" | |
let range = advance(welcome.endIndex, -6) ..< welcome.endIndex | |
welcome.removeRange(range) | |
// comapring strings | |
let quotation = "haha" | |
let sameQuotation = "haha" | |
quotation == sameQuotation | |
let eAcuteQuestion = "Voulez-vous un caf\u{E9}?" | |
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?" | |
eAcuteQuestion == combinedEAcuteQuestion | |
let latinCapitalLetterA: Character = "\u{41}" | |
let cyrillicCapitalLetterA: Character = "\u{0410}" | |
latinCapitalLetterA == cyrillicCapitalLetterA | |
// prefix and suffix equality | |
var testArray = [String]() | |
for index in 0..<10 { | |
testArray.append("Header\(index) \(index)Tailer") | |
} | |
var headerCount = 0 | |
var tailerCount = 0 | |
for message in testArray { | |
if message.hasPrefix("Header") { | |
++headerCount | |
} | |
if message.hasSuffix("Tailer") { | |
++tailerCount | |
} | |
} | |
headerCount | |
tailerCount | |
// Unicode Representations of Strings | |
let dogString = "Dog!!\u{1F436}" | |
var resultForUTF8 = [UInt8]() | |
for codeUnit in dogString.utf8 { | |
resultForUTF8.append(codeUnit) | |
} | |
resultForUTF8 | |
var resultForUTF16 = [UInt16]() | |
for codeUnit in dogString.utf16 { | |
resultForUTF16.append(codeUnit) | |
} | |
resultForUTF16 | |
var resultForUnicodeScalars = [UnicodeScalar]() | |
for codeUnit in dogString.unicodeScalars { | |
resultForUnicodeScalars.append(codeUnit) | |
} | |
resultForUnicodeScalars | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment