Sunday, 26 February 2017

String insert, remove, compare



1. Insert
//insert
    var welcome = "hello";
print(welcome);
    welcome.insert("!", at: welcome.endIndex);
    // welcome now equals "hello!"
     print(welcome);
    welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex));

    // welcome now equals "hello there!"

2. Remove
// remove
    welcome.remove(at: welcome.index(before: welcome.endIndex));
    // welcome now equals "hello there"
     print(welcome);
    let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
    welcome.removeSubrange(range);
print(welcome);
    // welcome now equals "hello"

// String and character equality

3. Compare

    let quotation = "We're a lot alike, you and I."
    let sameQuotation = "We're a lot alike, you and I."
    if quotation == sameQuotation {
        print("These two strings are considered equal")
    }
    // Prints "These two strings are considered equal"

No comments:

Post a Comment