StringTest

//: Playground – noun: a place where people can play

// Swift2 string blog

// https://developer.apple.com/swift/blog/?id=30

// Kyoung Shin Park (2016 Fall)

import Cocoa

var letters: [Character] = [“c”, “a”, “f”, “e”]

var cafe = String(letters)

print(letters.count)

print(cafe)

print(cafe.characters.count)

// print char of string “cafe”

for c in cafe.characters {

print(c)

}

let acuteAccent : Character = “\u{0301}”

cafe.append(acuteAccent)

print(cafe)

print(cafe.characters.count)

print(cafe.characters.last!)

cafe.characters.contains(“e”)

cafe.characters.contains(“´”)

cafe.characters.contains(“é”)

// In Swift, strings are considered equal regardless of whether they are constructed from decomposed or precomposed character sequences:

let decomposed = “\u{1100}\u{1161}” // ᄀ + ᅡ

let precomposed = “\u{AC00}” // 가

decomposed == precomposed // true

let string1 = “”

var string2: NSString = “Hello”

switch (string2) {

case let string2 as NSString:

print(“string2 is NSString”)

case let string2 as String:

print(“string2 is String”)

default:

print(“None of those”)

}

var len = string2.length

var string3 = String(“Hello”)

string3 += ” World”

string3.append(Character(“!”))

var string4 = String(count: 7, repeatedValue: Character(“A”))

var string5 = NSString()

var string9 = NSString(string: “Hello”)

var string6 = NSMutableString(capacity: 10)

var total = 100

var string7 = “\(string3)! total=\(total)”

var string8: NSString = string7

//var string10: String = string3.stringByAppendingFormat(” append %@”, total)

//print(string10)

string5 = string2.stringByAppendingFormat(” append %@”, string8)

print(string8)

print(string8.description)

var str = “Hello”

var world = ” World”

var smile = “?”

var mark = “!~”

var combined = str + mark

var combined2 = (str + world).stringByAppendingString(smile)

var len2 = combined2.characters.count

// length

var length = combined.characters.count

var len3 = combined.startIndex.distanceTo(combined.endIndex)

var len4 = (combined as NSString).length

// print char of string

for char in combined.characters {

print(char)

}

print(combined2.lowercaseString)

print(combined2.uppercaseString)

print(combined2.capitalizedString)

if let range = combined.rangeOfString(“ello”) { // contains

print(“ello”)

}

else {

print(“none”)

}

var trail = combined.substringWithRange(Range<String.Index>(start: str.endIndex, end: combined.endIndex))

print(“combined = \(combined)”)

let startIndex = combined.startIndex.advancedBy(2)

let endIndex = combined.startIndex.advancedBy(5)

//combined.insert(“^”, atIndex: combined.endIndex)

//let question : Character = “?”

//combined.append(question)

//print(combined)

combined.insert(“a”, atIndex: startIndex)

combined.insertContentsOf(“bc”.characters, at: startIndex.successor())

combined.removeAtIndex(combined.endIndex.predecessor())

print(combined)

let substring = combined[startIndex..<endIndex]

print(“substring = \(substring)”)

var sub2 = combined.substringWithRange(Range<String.Index>(start: startIndex, end: endIndex))

print(sub2)

var myString = “Seoul, New York, Tokyo LA”

var myArray = myString.componentsSeparatedByString(“,”)

for s in myArray {

print(s)

}

var myArray2 = myString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: “, “))

for s in myArray2 {

print(s)

}

var num = “56.25”

if let range = num.rangeOfString(“.”) {

let wholeNum = num[num.startIndex..<range.startIndex]

print(“wholeNum=\(wholeNum)”)

num.replaceRange(num.startIndex..<range.startIndex, with: “32”)

print(“num=\(num)”)

num.removeRange(num.startIndex..<range.startIndex)

print(“num2=\(num)”)

}

let animals = [“Giraffe”, “Cow”, “Dog”, “Cat”]

//animals.append(“Bird”)

var animal = animals[2]

for (index, value) in animals.enumerate() {

print(“animals[\(index)]=\(value)”)

}