码迷,mamicode.com
首页 > 编程语言 > 详细

Swift中自定义SubString

时间:2015-09-15 18:00:22      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

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"

 

Swift中自定义SubString

标签:

原文地址:http://www.cnblogs.com/rambot/p/4810629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!