Swift 3 String and Character Variables
These are formatted so you can copy the code straight into your Swift Playground files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// variable of Character type that is just a single character var singleLetter:Character = "F" // string type variable, contains as many letters, numbers, etc as you want var mishMash:String = "sdf2325&803!!~" // create an array of Characters var letters:[Character] = ["C", "A","R", "T","O", "O","N", "S","M", "A","R", "T", ".", "c", "o", "m"] //convert the letters array into a string variable named website var website:String = String (letters) // lets play around.... print (letters.count) // prints the number of objects in the array print (website.characters.count ) //prints the same value //test to see if converting it all to lower case equals "cartoonsmart.com" if ( website.lowercased() == "cartoonsmart.com") { print (website.lowercased()) //note in Swift 3 this is lowercased(). In Swift 2 it was lowercaseString } let capFirstLetters = website.capitalized //capFirstLetters would be "Cartoonsmart.Com" //note in Swift 3 this is .capitalized. In Swift 2 it was capitalizedString //test to see if website contains a range of ".com" if ( website.range(of: ".com") != nil) { //note in Swift 2, this would have been .rangeOfString(".com") print ("it has the dot com") } if ( website.lowercased().hasPrefix("car")) { //do something if the string begins with "car" print ("true") } if ( website.hasSuffix(".net")) { // do something if its .net print ("true") } else { //its not .net print ("not true") } |
How to split up a long string into shorter strings without breaking up words…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
var longString:String = "There was a disturbance in the Force last night. Dispatch Jedi to check it out." let maxInOneLine:Int = 30 //max characters in the first line var i:Int = 0 var shorterString1:String = "" var shorterString2:String = "" var useLine2:Bool = false //if true, we will put the rest of the string characters on line 2 //iterate through the longString one letter at a time for letter in longString.characters { //if i is greater than 30 and the current letter is not a space... if ( i > maxInOneLine && String(letter) == " " ){ useLine2 = true } if (useLine2 == false){ shorterString1 = shorterString1 + String(letter) } else { shorterString2 = shorterString2 + String(letter) } i += 1 } print( shorterString1 ) // There was a disturbance in the Force print( shorterString2 ) // last night. Dispatch Jedi to check it out. |
How to Convert String to Int or Int to String with Swift 3…
1 2 3 4 5 6 7 8 9 10 |
//here are a couple ways to convert an Int to a String let someString:String = "Obi Won" let someInt:Int = 40 let someSentence:String = "Obi Won was \(someInt) when he retired to Tattoine" let someSentence2:String = "Old Ben was " + String(someInt + 20) + " when he secretly killed Aunt Beru and Uncle Owen" //lets reverse it and convert a String to a number let someNumberString:String = "30" let someNumber:Int = Int(someNumberString)! //30 |
If you’re looking for places to learn after these Swift Playground examples, check out CartoonSmart.com or SpriteKitLessons.com