Swift中自定义SubString

extension String{

    func subString(from fromIndex: Int, to toIndex: Int) -> String{
        var halfString = self.substringWithRange(Range<String.Index>(start: advance(self.startIndex, fromIndex), end: advance(self.startIndex, toIndex)))
        return halfString
    }
    
    func subString(from location: Int, length strLength: Int) -> String{
        let nsForm = self as NSString
        return nsForm.substringWithRange(NSRange(location: location, length: strLength))
        
    }
    
}

var testStr = "Hello World"
var helloStr = testStr.subString(from: 0, length: 5)   //"hello"
var helloStr1 = testStr.subString(from: 0, to: 5)      //"hello"

 

posted @ 2015-09-15 16:22  Rambot  阅读(210)  评论(0编辑  收藏  举报