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"