ios8

Swift 2.0 substringWithRange

by
published on
Ah finally solved. This was so much drama for a small thing to do. Problem: I have a string; SINCGKXXXYY . [Say these are 2 airport destinations + some junk text and i want to separate the airports out of the string in the easiest possible way!]   Use the following extension:
extension String {
    
    subscript (i: Int) -> Character {
        return self[self.startIndex.advancedBy(i)]
    }
    
    subscript (i: Int) -> String {
        return String(self[i] as Character)
    }
    
    subscript (r: Range<Int>) -> String {
        return substringWithRange(Range(start: startIndex.advancedBy(r.startIndex), end: startIndex.advancedBy(r.endIndex)))
    }
}
Once done you can use it using this  
var myCrappyJunkText:String = "SINCGKXXXYY"

var firstCode = myCrappyJunkText[0...2]  //Should hold SIN

var 2ndCode = myCrappyJunkText[3...5]  //Should hold CGK
Cheers!