Skip to content

Instantly share code, notes, and snippets.

@Harryyan
Created December 22, 2014 08:16
Show Gist options
  • Save Harryyan/4eda1276e096d93e4345 to your computer and use it in GitHub Desktop.
Save Harryyan/4eda1276e096d93e4345 to your computer and use it in GitHub Desktop.
extension String{
//下标属性
subscript(subrange: Range<Int>) -> String {
get {
var start = advance(startIndex, subrange.startIndex)
var end = advance(startIndex, subrange.endIndex)
return substringWithRange(Range(start: start, end: end))
}
set {
var start = advance(startIndex, subrange.startIndex)
var end = advance(startIndex, subrange.endIndex)
var updateRange = Range(start: start, end: end)
self.replaceRange(updateRange, with: newValue)
}
}
subscript(index:Int) -> String {
get{
return self[Range(start: index, end: index+1)]
}
set{
self[Range(start:index,end:index+1)]=newValue;
}
}
subscript(start:Int,length:Int)->String{
get{
return self[Range(start:start,end:start+length+1)]
}
set{
self[Range(start:start,end:start+length+1)]=newValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment