These are formatted so you can copy the code straight into your Swift Playground files.
/// 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")
}
refrence- http://swift3tutorials.com/
No comments:
Post a Comment