String Theory
Syntax:let a = "I am a string"
A string is an ordered-collection of characters. The String type in Swift is similar in many ways to the NSString class from Objective-C. In fact, the entire API for NSString is available to the String type in Swift. One difference between Objective-C and Swift is that in Swift a string is mutable or not based on whether it is a variable or constant, whereas Objective-C used the NSMutableString class. Another difference is that in Swift, String is a value type; meaning that the value is copied whenever a string is passed to a function or method, or assigned to a constant or variable. In Objective-C, the NSString would pass a reference to a single NSString object.
/* To create an empty String, the following two options can be used */
var empty1 = ""
var empty1 = String()
// To concatenate Strings, the + sign can be used
var empty1 = "Hello"
var string1 = empty1 + " World!"
// using the utf16Count property to return the number of Unicode characters in a String:
var greeting: String = "Hello. How are you?"
println("There are \(greetings.utf16Count) Unicode characters in the greeting String")
//prints "There are 19 Unicode characters in the greeting String"
//using the isEmpty property to determine if a String has a value
if greeting.isEmpty{
println("The greeting String is Empty")
} else {
println("The greeting String has values")
}
//use the append() method to add Character or UnicodeScalar to the end of a String
var test = "this is a test!"
var charca: Character = "a"
var unic: UnicodeScalar = "b"
test.append(charca)
println(test)
//prints 'this is a test!a'
test.append(unic)
println(test)
//prints 'this is a test!ab'
//use the capitalizedString, lowercaseString, and uppercaseString methods to change a String
var test2 = "this IS not A test!"
println("\(test2.capitalizedString)")
//prints 'This Is Not A Test!'
println("\(test2.lowercaseString)")
//prints 'this is not a test!'
println("\(test2.uppercaseString)")
//prints 'THIS IS NOT A TEST!'
//use substringFromIndex and substringToIndex to return part of a String
var test3 = "Why do we have so many tests?"
var StrIndex = advance(test3.startIndex, 6)
println(test3.substringFromIndex(StrIndex))
//prints ' we have so many tests?'
println(test3.substringToIndex(StrIndex))
//prints 'Why do'
//using the += operator to add a new String to the existing String
var test = "Hello... "
test += "Miss Lady"
//the String is now 'Hello... Miss Lady'
//using the less than and greater than operators to compare two Strings
var str1 = "hello"
var str2 = "goodnight"
println(str1 < str2)
//prints false
println(str1 > str2)
//prints true